您的位置:首页 > 其它

POJ 2352 Stars 树状数组

2017-09-10 11:01 337 查看
原题链接

由于是求左下角的总数,且y输入从小到大,满足前缀和,故使用树状数组

#include <stdio.h>
#include <string.h>
#define lowbit(x) (x&(-x))
const int maxn=15010,maxx=32010;
int C[maxx],N,ans[maxn],x,y;
///the level of stars
int sum(int x){
int ans=0;
while(x>0){
ans+=C[x];
x-=lowbit(x);
}
return ans;
}

void add(int x){
while(x<=maxx){
C[x]++;
x+=lowbit(x);
}
}

int main()
{
scanf("%d",&N);
memset(ans,0,sizeof(ans));
memset(C,0,sizeof(C));
for(int i=0;i<N;i++){
scanf("%d%d",&x,&y);
ans[sum(x+1)]++;///because x can equal 0, and array C need x>=1, so use x+1
add(x+1);
}
for(int i=0;i<N;i++)
printf("%d\n",ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm poj 树状数组