题目链接:http://poj.org/problem?id=2352
题目大意:给n个星星的坐标,对于每个星星记录有多少个星星在他的左下方。
题目思路:因为给的星星的坐标是按照y坐标升序的,那么就说明后边的星星一定比前边的“高,所以说统计前边有多少个星星比这个星星靠左就行了。
题目坑点:这个x的范围是0到32000,但是树状数组求不了下标为0的点,所以x要加一后再操作,当然add函数中的x<=32000也要加一,不然也会WA。
#include<iostream>
#include<string.h>
#include<stdio.h>
#define ll long long
using namespace std;
#define lowbit(x) x&(-x)
ll ans[15005];
ll c[32099],cnt[15005];
ll ask(ll x){
ll ans=0;
for(;x;x-=lowbit(x))ans+=c[x];
return ans;
}
void add(ll x,ll y){
for(;x<=32001;x+=lowbit(x))c[x]+=y;
}
int main()
{
ll n;
while(~scanf("%lld",&n)){
memset(cnt,0,sizeof(cnt));
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
for(ll i=1;i<=n;i++){
ll x,y;
scanf("%lld%lld",&x,&y);
x++;
ans[i]=ask(x);
add(x,1);
cnt[ans[i]]++;
}
for(ll i=0;i<=n-1;i++){
printf("%lld\n",cnt[i]);
}
}
}