poj 2352 Stars (树状数组)

题目:http://poj.org/problem?id=2352

题意:给出n个星星的坐标, 每个星星的左下角有几个星星,该星星的level 就是几。

求level为0~n-1的个数。

本来想用线段树做,但是线段树还没理解好,做不出来, 就用树状数组做了。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 32010;
 7 int c[maxn], lev[15010];
 8 
 9 void init()
10 {
11     for(int i = 1; i < maxn; i++)
12         c[i] = 0;
13     for(int i = 1; i <= 15010; i++)
14     lev[i] = 0;
15 
16 }
17 int lowbit(int x)
18 {
19     return x&(-x);
20 }
21 void add(int x)
22 {
23     while(x <= maxn)
24     {
25         c[x]++;
26         x +=lowbit(x);
27     }
28 }
29 int sum(int x)
30 {
31     int ret = 0;
32     while(x > 0)
33     {
34         ret += c[x];
35         x -= lowbit(x);
36     }
37     return ret;
38 }
39 
40 int main()
41 {
42     int n,x,y,i;
43     while(~scanf("%d",&n))
44     {
45         init();
46         for(i = 1; i <= n; i++)
47         {
48             scanf("%d%d", &x, &y);
49             x++;
50             lev[sum(x)]++;
51             add(x);
52         }
53         for(i = 0; i < n; i++)
54             printf("%d\n",lev[i]);
55     }
56     return 0;
57 }

 

posted @ 2014-02-18 10:41  水门  阅读(143)  评论(0编辑  收藏  举报