【HDU1514】Stars(树状数组)

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

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <cctype>
#include <cmath>
using namespace std; const int V = + ;
const int M = + ;
int C[V], res[M]; int lowbit (int x) {
return x & -x;
} int sum (int x) {
int ret = ;
while (x > ) {
ret += C[x]; x -= lowbit(x);
}
return ret;
} void add (int x, int d) {
while (x <= V) {
C[x] += d; x += lowbit(x);
}
} int main () {
int n, x, y;
while (~scanf("%d", &n)) {
memset(C, , sizeof(C));
memset(res, , sizeof(res)); for (int i = ; i < n; ++ i) {
scanf ("%d %d", &x, &y);
res[sum(x + )] ++;
add(x + , );
}
for (int i = ; i < n; ++ i) {
printf ("%d\n", res[i]);
}
}
return ;
}
上一篇:c语言排序算法总结


下一篇:C#_扩展方法