【樹狀陣列 區間修改,單點求值】1556 Color the ball

CN_swords發表於2017-09-06

Link:http://acm.split.hdu.edu.cn/showproblem.php?pid=1556

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;

#define lowbit(i) (i&(-i))
const int N = 100010;
int tree[N];
int n;
void add(int x,int val){
    while(x){
        tree[x] += val;
        x -= lowbit(x);
    }
}
int query(int x){
    int ans = 0;
    while(x<=n){
        ans += tree[x];
        x += lowbit(x);
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n) && n){
        memset(tree,0,sizeof(tree));
        for(int i = 0; i < n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(b,1);
            add(a-1,-1);
        }
        for(int i = 1; i <= n; i++)
            printf("%d%c",query(i),i==n?'\n':' ');
    }
    return 0;
}


相關文章