HDU1556(线段树)

代码:

#include<stdio.h>
#define maxn 1000005
int nn;
struct node
{
    int l,r,sum;
}str[maxn];
void build(int l,int r,int n)
{
    str[n].l=l;
    str[n].r=r;
    str[n].sum=0;
    if(l==r)
        return;
    int temp=(l+r)/2;
    build(l,temp,2*n);
    build(temp+1,r,2*n+1);
}
void color(int a,int b,int n)
{
    if(str[n].l==a&&str[n].r==b)
    {
        str[n].sum++;
        return;
    }
    int temp=(str[n].l+str[n].r)/2;
    if(b<=temp)
        color(a,b,2*n);
    else if(a>temp)
        color(a,b,2*n+1);
    else
    {
        color(a,temp,2*n);
        color(temp+1,b,2*n+1);
    }
}
void out(int n,int sum)
{
    if(str[n].l==str[n].r)
    {
        printf("%d",sum+str[n].sum);
        if(str[n].l!=nn)
            printf(" ");
        return;
    }
    out(2*n,sum+str[n].sum);
    out(2*n+1,sum+str[n].sum);
}
int main()
{
    while(scanf("%d",&nn),nn!=0)
    {
        build(1,nn,1);
        for(int i=1;i<=nn;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            color(a,b,1);
        }
        out(1,0);
        printf("\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值