poj 3067(树状数组)

点击打开链接


题意:

日本东岸有n个城市,西岸有m个城市,每个城市,从北到南编号为1,2,..,两岸的城市之间有k条高速路,求这k条高速路有多少焦点。。。


跟stars是一个类型的,先对第1个数从大到小,对第二个数从大到小。。

这样ans用int64,还有k最大为1000*1000


#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define N 1000011
int C[N];
struct node
{
	int x,y;
}A[N];
int cmp(node a,node b)
{
	if(a.x!=b.x)return a.x>b.x;
	return a.y>b.y;
}
int bit(int x)
{
	return x&(-x);
}
int sum(int x)
{
	int ans=0;
	while(x)
	{
		ans+=C[x];
		x-=bit(x);
	}
	return ans;
}
void add(int x,int a)
{
	while(x<N)
	{
		C[x]+=a;
		x+=bit(x);
	}
}
int main()
{
	int k,t;
	int n,m;
	int i,T;
	__int64 ans;
	t=1;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i<k;i++)
			scanf("%d%d",&A[i].x,&A[i].y);
		sort(A,A+k,cmp);
	//	for(i=0;i<k;i++)
	//		printf("%d%d\n",A[i].x,A[i].y);
		memset(C,0,sizeof(C));
		ans=0;
		for(i=0;i<k;i++)
		{
			ans+=sum(A[i].y-1);
			add(A[i].y,1); 
		}
		printf("Test case %d: %I64d\n",t++,ans);
	}
	return 0;
}
	


你可能感兴趣的:(poj,树状数组)