HDU1556-Color the ball

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

#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAX 100001
int tree[MAX<<2];; 
void build(int l,int r,int rt)
{
    tree[rt]=0;
    if(l==r) 
       return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt]++;
        return;
    }
    int m=l+r>>1;
    if(L<=m)
       update(L,R,lson);
    if(R>m)
       update(L,R,rson);
}
int query(int p,int l,int r,int rt)
{
    if(l==r)
       return tree[rt];
    if(tree[rt]>0)
    {
        tree[rt<<1]+=tree[rt];
        tree[rt<<1|1]+=tree[rt];
        tree[rt]=0;
    }
    int m=(l+r)>>1;
    if(p<=m) 
       return query(p,lson);
    if(p>m) 
       return query(p,rson);
}
int main(void)
{
    int N,a,b,i;
    while(scanf("%d",&N),N)
    {
        build(1,N,1);
        for(i=1;i<=N;i++)
        {
            scanf("%d%d",&a,&b);
            update(a,b,1,N,1);
        }
        for(i=1;i<N;i++) 
           printf("%d ",query(i,1,N,1));
        printf("%d\n",query(N,1,N,1));
    }
    return 0;
}
posted @ 2012-08-31 11:00  Yogurt Shen  阅读(159)  评论(0编辑  收藏  举报