## Sunday, December 27, 2015

### poj 2352 Stars 树状数组

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
```5
1 1
5 1
7 1
3 3
5 5```
Sample Output
```1
2
1
1
0```
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

1. const int MAXN = 32005;
2.
3. int c[MAXN],level[MAXN],n;
4.
5. int lowbit(int x){return x & (-x);}
6.
7. // 求前n项的和
8. int sum(int n){
9.     int sum = 0;
10.     while(n > 0){
11.         sum += c
12. ;
13.         n -= lowbit(n);
14.     }
15.     return sum;
16. }
17. // 增加某个元素的大小
19.     while(x <= MAXN){
20.         ++c[x];
21.         x += lowbit(x);
22.     }
23. }
24.
25. int main(){
26.     int n,x,y;
27.     while(~scanf("%d",&n)){
28.         memset(level, 0, sizeof(level));
29.         memset(c, 0, sizeof(c));
30.         for(int i=0; i<n; ++i) {
31.             scanf("%d%d",&x,&y);
32.             ++x;
33.             level[sum(x)]++;
35.         }
36.         for(int i=0; i<n; ++i)
37.             printf("%d\n",level[i]);
38.     }
39.     return 0;
40. }

2. 线段树
1. const int MAXN = 32005;
2.
3. int sum[MAXN<<2],level[MAXN<<2];
4.
5. void update(int rt,int left,int right,int data){
6.     ++sum[rt];
7.     if(left==right) return;
8.     if(data <= mid) update(lson,data);
9.     else update(rson,data);
10. }
11. int query(int rt,int left,int right,int l,int r){
12.     if(left==l && right==r) {
13.         return sum[rt];
14.     }
15.     int m = mid;
16.     if(r <= m) return query(lson,l,r);
17.     else if(l > m) return query(rson,l,r);
18.     else return query(lson,l,m)+query(rson,m+1,r);
19. }
20.
21. int main(){
22.     int n,x,y;
23.     while(~scanf("%d",&n)){
24.         memset(sum, 0, sizeof(sum));
25.         memset(level, 0, sizeof(level));
26.         for(int i=0; i<n; ++i){
27.             scanf("%d%d",&x,&y);
28.             ++x;
29.             ++level[query(1,1,MAXN,1,x)];
30.             update(1,1,MAXN,x);
31.         }
32.         for(int i=0; i<n; ++i)
33.             printf("%d\n",level[i]);
34.     }
35.     return 0;
36. }
http://www.51itong.net/poj-2352-stars-or-7165.html

 `08` `const` `int` `MAXN = 32005;`
 `09` `const` `int` `N = 15005;`
 `10` `#define lowbit(x) (x&(-x))`
 `11` `int` `tree[MAXN];`
 `12` `int` `ans[N];`
 `13` `int` `sum(``int` `i)`
 `14` `{`
 `15` `    ``int` `s=0;`
 `16` `    ``while``(i>0){`
 `17` `        ``s+=tree[i];`
 `18` `        ``i-=lowbit(i);`
 `19` `    ``}`
 `20` `    ``return` `s;`
 `21` `}`
 `22`
 `23` `void` `add(``int` `i)`
 `24` `{`
 `25` `    ``while``(i<=MAXN-1){ ``//防止正好算出了MAXN，因为tree的下标最大也只有MAXN-1`
 `26` `        ``tree[i]++;`
 `27` `        ``i+=lowbit(i); ``//i>0 否则死循环`
 `28` `    ``}`
 `29` `}`
 `30`
 `31` `int` `main()`
 `32` `{`
 `33` `    ``int` `n;`
 `34` `    ``scanf``(``"%d"``,&n);`
 `35` `    ``for``(``int` `i=0;i
 `36` `        ``int` `x,y;`
 `37` `        ``scanf``(``"%d%d"``,&x,&y);`
 `38` `        ``ans[sum(++x)]++;`
 `39` `        ``add(x);`
 `40` `    ``}`
 `41` `    ``for``(``int` `i=0;i
 `42` `        ``printf``(``"%d\n"``,ans[i]);`
 `43` `    ``return` `0;`
 `44` `}`