hdu1968------------线段树

这段时间不怎么写文章了,但没偷懒。一直刷题,学习哦!

比如这一集是我第三道线段树,嘿嘿,越来越明白了。不错

 

#include<stdio.h>
struct node
{
    int l;
    int r;
    int num;
}nod[400005];

void build(int l,int r,int n)
{
    nod[n].l=l;
    nod[n].r=r;
    nod[n].num=1;
    if(l==r)
    return ;
    int mid=(l+r)/2;
    build(l,mid,n*2);
    build(mid+1,r,n*2+1);
}
void updata(int l,int r,int c,int n)
{
    if(l<=nod[n].l&&nod[n].r<=r)
    {
        nod[n].num=c;
        return ;
    }
    if(nod[n].num!=-1)
    {
        nod[n*2].num=nod[n].num;
        nod[n*2+1].num=nod[n].num;
        nod[n].num=-1;
    }
    int mid=(nod[n].l+nod[n].r)/2;
    if(r<=mid)
    updata(l,r,c,n*2);
    else if(l>mid)
    updata(l,r,c,n*2+1);
    else
    {
        updata(l,mid,c,n*2);
        updata(mid+1,r,c,n*2+1);
    }
}
int quary(int l,int r,int n)
{
    if(nod[n].num!=-1)
    return nod[n].num*(r-l+1);
    int mid=(l+r)/2;
    
    if(r<=mid)
    return quary(l,r,n*2);
    else if(l>mid)
    return quary(l,r,n*2+1);
    else
    {

        return quary(l,mid,n*2)+quary(mid+1,r,n*2+1);
    }
}
int main()
{
    int a,b,c;
    int t,n,m,i,j;
    scanf("%d",&t);
    int t1=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            updata(a,b,c,1);
        }
        t1++;
        printf("Case %d: The total value of the hook is %d.\n",t1,quary(1,n,1));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zhangyabin---acm/archive/2012/05/19/2509209.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值