HDU 1556 树状数组

#include
#include
#include
using namespace std;
const int maxn = 1e5;
int n;
int lowbit(int x){
	return x&(-x);
}
void print2(int x){
	int i;
//	for(i = 31;i >= 0;--i) if(x & (1 << i) != 0) break;
//	printf(" --  %d\n",i);
	for(i = 31;i >= 0; --i) printf("%d",(x >> i)&1); 
	printf("\n");
}
int c[maxn+10];
void updata(int x,int k){
	while(x > 0){
		c[x] += k;
		x -= lowbit(x);
	}
}
int getSum(int x){
	int ans = 0;
	while(x <= n){
		ans += c[x];
		x += lowbit(x);
	}
	return ans;
}
int main(){
	while(cin >> n,n){
		memset(c,0,sizeof(c));
		int x,y;
		for(int i = 0;i < n;++i){
			scanf("%d%d",&x,&y);
			updata(x-1,-1);
			updata(y,1);
		}
		for(int i = 1;i < n;++i) printf("%d ",getSum(i));
		printf("%d\n",getSum(n));
	}
	return 0;
}

你可能感兴趣的:(ACM程序设计)