http://poj.org/problem?id=2352
题意:
给出每个星星的x、y坐标,计算每颗星星左方和正下方的星星个数。
思路:
由于星星是根据y坐标递增的基础上再根据x坐标递增的顺序给出,所以我们只需要考虑横坐标上的星星情况,至于正下方的星星个数,我们只需要记录一下最后加上即可。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<queue> 7 #include<cmath> 8 #include<map> 9 #include<stack> 10 using namespace std; 11 12 const int maxn=32000+5; 13 14 int n; 15 int c[maxn]; 16 int ans[maxn]; 17 int num[maxn]; 18 19 int lowbit(int x) 20 { 21 return x&-x; 22 } 23 24 int sum(int x) 25 { 26 int ret=0; 27 while(x>0) 28 { 29 ret+=c[x]; 30 x-=lowbit(x); 31 } 32 return ret; 33 } 34 35 void add(int x,int d) 36 { 37 while(x<=maxn) 38 { 39 c[x]+=d; 40 x+=lowbit(x); 41 } 42 } 43 44 int main() 45 { 46 //freopen("D:\input.txt","r",stdin); 47 scanf("%d",&n); 48 for(int i=0;i<n;i++) 49 { 50 int x,y; 51 scanf("%d%d",&x,&y); 52 x++; //注意这儿需要+1,因为x为0也是有可能的 53 add(x,1); 54 int p=sum(x-1); 55 p+=num[x]; //加上正下方的星星个数 56 ans[p]++; 57 num[x]++; 58 } 59 for(int i=0;i<n;i++) 60 printf("%d ",ans[i]); 61 return 0; 62 }