求每个星星坐下方的星星个数,题目已经按y的左边递增的顺序排好
简单的区间统计, 一定要注意 i可以为0的情况的存在!!
(树状数组)
#include <stdio.h> #include <string.h> #define N 15000 + 10 int c[32000+10]; int n, Max; inline int lowbit(int x) {return (x&-x);} void modify(int i,int d) {for(; i<=Max+1; i+=lowbit(i)) c[i]+=d;} int sum(int i) {int ret = 0; for(; i>0; i-=lowbit(i)) ret+=c[i]; return ret;} int main() { int i, y; int x[N], ans[N]; while(scanf("%d",&n)!=EOF) { Max = 0; for(i=0; i<n; ++i) { scanf("%d%d",&x[i],&y); if(Max < x[i]) Max = x[i]; } memset(c, 0, sizeof c ); memset(ans, 0, sizeof ans ); for(i=0; i<n; ++i) { ans[sum(x[i]+1)]++; modify(x[i]+1, 1); } for(i=0; i<n; ++i) { printf("%d\n",ans[i]); } } return 0; }