poj 2481(树状数组)

这题树状数组明显可解,不解释,但是注意有相同的奶牛的处理

代码

#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#define N 100005
using namespace std;
int c[N],tem;
struct dian
{
	int x,y,z;
}d[N];
int cmp(dian a,dian b)
{
	if(a.x==b.x)
		return a.y<b.y;
	return a.x>b.x;
}
int lowbit(int n)
{
	return n&(-n);
}
int sum(int n)
{
	int ans=0;
	while(n)
	{
		ans+=c[n];
		n-=lowbit(n);
	}
	return ans;
}
void update(int n,int k)
{
	for(int i=n;i<=tem;i+=lowbit(i))
	{
		c[i]+=k;
	}
}
int main()
{
	int n,count[N],i;
	while(1)
	{
		memset(c,0,sizeof(c));
		tem=-0x7fffffff;
		scanf("%d",&n);
		if(n==0)
			return 0; 
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&d[i].y,&d[i].x);
			d[i].x++,d[i].y++;
			d[i].z=i;
			if(tem<d[i].y)
				tem=d[i].y;
		}
		sort(d,d+n,cmp);
		for(i=0;i<n;i++)
		{
			if(d[i].x==d[i-1].x&&d[i].y==d[i-1].y)
				count[d[i].z]=count[d[i-1].z];
			else
				count[d[i].z]=sum(d[i].y);
			update(d[i].y,1);
		}
		for(i=0;i<n;i++)
		{
			printf("%d ",count[i]);
		}
		printf("\n");
	}
	return 0; 
}


你可能感兴趣的:(c,ini)