标签:树状数组
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
1 1 1 3 2 1
代码:
#include <stdio.h>
#include <string.h>
#define M 100005
int c[M], n;
int lowbit(int x){
return x&(-x);
}
int getsum(int x){
int sum = 0;
while(x){
sum += c[x];
x -= lowbit(x);
}
return sum;
}
void add(int x, int val){
while(x<=n){
c[x] += val;
x += lowbit(x);
}
}
int main(){
while(scanf("%d", &n), n){
memset(c, 0, sizeof(c));
int a, b, m = n;
//n += 1;
while(m --){
scanf("%d%d", &a, &b);
add(a, 1);
add(b+1, -1);
}
printf("%d", getsum(1));
for(int i = 2; i <= n; i ++){
printf(" %d", getsum(i));
}
printf("\n");
}
return 0;
}
hdoj 1556 Color the ball 【树状数组】
标签:树状数组
原文地址:http://blog.csdn.net/shengweisong/article/details/40566071