Poj--2352(树状数组)

2014-09-08 15:51:22

思路:直接树状数组,这题给我的启示是:树状数组要从1开始编号,而不能从0开始,因为lowbit(0) == 0,会导致无限循环。

 1 /*************************************************************************
 2     > File Name: p2352.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Mon 08 Sep 2014 03:27:37 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 const int INF = 1 << 29;
19 const int maxn = 32005;
20 
21 int N;
22 int x,y;
23 int c[maxn + 5];
24 int ra[15005];
25 
26 int Lowbit(int x){return x & (-x);}
27 void Update(int x,int d){while(x <= maxn){ c[x] += d,x += Lowbit(x);}}
28 int Getsum(int x){int res = 0;while(x){res += c[x],x -= Lowbit(x);}return res;}
29 
30 int main(){
31     while(scanf("%d",&N) != EOF){
32         memset(ra,0,sizeof(ra));
33         memset(c,0,sizeof(c));
34         for(int i = 1; i <= N; ++i){
35             scanf("%d%d",&x,&y);
36             ++x;
37             Update(x,1);
38             ra[Getsum(x) - 1]++;
39         }
40         for(int i = 0; i < N; ++i) printf("%d\n",ra[i]);
41     }
42     return 0;
43 }
44         

 

posted @ 2014-09-08 16:03  Naturain  阅读(94)  评论(0编辑  收藏  举报