POJ3067--Japan(樹狀數組)

題意就是讓你求交點有多少個。

我們可以先按a從小到大排序,a相等就按b從小到大排序。

代碼如下:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#define M 1005
#define LL long long
using namespace std;

int n,m,k;
int c[M];
struct node
{
	int x,y;
	bool operator < ( const node &h ) const
	{
		return x<h.x || (x==h.x && y<h.y);
	}
};
struct node p[M*M];

int Lowbit(int x)
{
	return x&(-x);
}

void Update(int x,int d)
{
	while(x<=M)
	{
		c[x]+=d;
		x+=Lowbit(x);
	}
}

int Sum(int x)
{
	int sum=0;
	while(x>0)
	{
		sum+=c[x];
		x-=Lowbit(x);
	}
	return sum;
}

int main()
{
	int t;
	scanf("%d",&t);
	for(int count=1;count<=t;count++)
	{
		memset(c,0,sizeof(c));
		scanf("%d%d%d",&n,&m,&k);
		for(int i=1;i<=k;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		sort(p+1,p+1+k);
		LL ans=0;
		for(int i=1;i<=k;i++)
		{
			Update(p[i].y,1);
			ans+=(i-Sum(p[i].y));
		}
		printf("Test case %d: %lld\n",count,ans);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章