HDU 1541 Stars (树状数组)

根据题意我们要维护前n-1个点中按照x值排序的前缀和,那么我们就可以用树状数组来维护一个大小为32000+的数组的前缀和,然后logn的更新,logn的查询就能过了。

#pragma warning(disable:4996)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 32005;
const int N = 15005;

int C[M], len = 32002;
int ans[N];

int lowbit(int x) {
	return x&(-x);
}

void update(int i, int v) {
	while (i <= len) {
		C[i] += v;
		i += lowbit(i);
	}
}

int sum(int i) {
	int ret = 0;
	while (i > 0) {
		ret += C[i];
		i -= lowbit(i);
	}
	return ret;
}

int main() {
	int n;
	while (~scanf("%d", &n)) {
		memset(C, 0, sizeof C);
		memset(ans, 0, sizeof ans);
		for (int i = 1; i <= n; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			x++; y++;
			ans[sum(x)]++;
			update(x, 1);
		}
		for (int i = 0; i < n; i++) {
			printf("%d\n", ans[i]);
		}
	}
	return 0;
}


你可能感兴趣的:(HDU 1541 Stars (树状数组))