poj2353树状数组

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int A[36000];
int N=36000, n;
int ans[36000];
int lowbit(int x) {
	return x & (-x);
}

int add(int v, int m) {
	while(v <= N) {
		A[v] += m;
		v += lowbit(v);
	}
}

int sum(int end) {
	int result = 0;
	while(end > 0) {
		result += A[end];
		end -= lowbit(end);
	}
	return result;
}
void print() {
	for (int i = 1; i <= N; i++)
		cout<<A[i]<<' ';
	cout<<endl;
}

int main() {

	scanf("%d", &n);
	int x, y;
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", &x, &y);
		ans[sum(x+1)]++;
		add(x+1, 1);
	}
	//print();
	for (int i = 0; i < n; i++)
		printf("%d\n", ans[i]);
	return 0;
}

你可能感兴趣的:(poj2353树状数组)