【HDU1514】Stars(树状数组)

绝对大坑。千万记住树状数组0好下标位置是虚拟节点。详见大白书P195。其实肉眼看也能得出,在add(有的也叫update)的点修改操作中如果传入0就会死循环。最后TLE。所以下标+1解决问题。上代码!

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <numeric>
 7 #include <cctype>
 8 #include <cmath>
 9 using namespace std;
10 
11 const int V = 32000 + 10;
12 const int M = 15000 + 10;
13 int C[V], res[M];
14 
15 int lowbit (int x) {
16     return x & -x;
17 }
18 
19 int sum (int x) {
20     int ret = 0;
21     while (x > 0) {
22         ret += C[x]; x -= lowbit(x);
23     }
24     return ret;
25 }
26 
27 void add (int x, int d) {
28     while (x <= V) {
29         C[x] += d; x += lowbit(x);
30     }
31 }
32 
33 int main () {
34     int n, x, y;
35     while (~scanf("%d", &n)) {
36         memset(C, 0, sizeof(C));
37         memset(res, 0, sizeof(res));
38 
39         for (int i = 0 ; i < n; ++ i) {
40             scanf ("%d %d", &x, &y);
41             res[sum(x + 1)] ++;
42             add(x + 1, 1);
43         }
44         for (int i = 0 ; i < n; ++ i) {
45             printf ("%d\n", res[i]);
46         }
47     }
48     return 0;
49 }

 

posted @ 2014-07-27 13:24  Desgard_Duan  阅读(188)  评论(0编辑  收藏  举报