/*
Subject: 树状数组
Author : a_clay
Created Date : 2012-02-02
Sample : poj 2352 Stars
*/
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <queue>
#include <algorithm>
#define Bug cout << "here\n";
using namespace std;
const int N = 15005;
const int M = 32005;
int B[M];
int cnt[N];
int n, T, k;
int lowbit(int x) {
return x & (-x);
}
void add(int i, int x) {
while(i <= M) { // 注意, 不是 n, 是 M
B[i] += x;
i = i + lowbit(i);
}
} //得到 Bn
long long getsum(int i) {
long long sum = 0;
while(i > 0) {
sum += B[i];
i = i - lowbit(i);
}
return sum;
}
int main() {
int x, y, i;
memset(cnt, 0, sizeof(cnt));
memset(B, 0, sizeof(B));
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
cnt[getsum(++x)]++;
add(x, 1);
}
for(i = 0; i < n; i++) {
cout << cnt[i] << endl;
}
system("pause");
return 0;
}
poj 2352 Stars (树状数组 + 1)
最新推荐文章于 2019-03-26 17:11:07 发布
本文详细介绍了一种高效的数据结构——树状数组,并通过实例poj2352 Stars问题展示了其在解决区间求和等问题上的应用。文章通过具体代码解释了树状数组的基本操作,如增加元素(add)和获取前缀和(getsum),并提供了完整的C++实现。
1465

被折叠的 条评论
为什么被折叠?



