POJ 2352-Stars-树状数组

把星星按照x坐标排序,然后依次插入,查询,这题跟japan一个套路。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 const int maxn = 32005;
 8 int N;
 9 int ans[maxn];
10 int c[maxn];
11 
12 int lowbit(int x)
13 {
14     return x&(-x);
15 }
16 
17 void update(int x)
18 {
19     while(x <= maxn)
20     {
21         c[x] ++;
22         x += lowbit(x);
23     }
24 }
25 
26 int getsum(int x)
27 {
28     int res = 0;
29     while( x > 0 )
30     {
31         res += c[x];
32         x -= lowbit(x);
33     }
34     return res;
35 }
36 
37 int main()
38 {
39     while(~scanf("%d",&N))
40     {
41         memset(c,0,sizeof c);
42         memset(ans,0,sizeof ans);
43 
44         for(int i=1,x,y;i<=N;i++)
45         {
46             scanf("%d%d",&x,&y);
47             x++;
48             ans[getsum(x)]++;
49             update(x);
50         }
51 
52         for(int i=0;i<N;i++)
53         {
54             printf("%d
",ans[i]);
55         }
56     }
57 }
原文地址:https://www.cnblogs.com/helica/p/5281508.html