hdu 1698 Just a Hook 线段树成段修改

#include <stdio.h>
#include <string.h>
#define maxn 100000
struct node{
    int l,r,w,to;//to记录该段的修改情况,0为未修改
}e[maxn*4];
int d[50005];
void build(int a,int b,int c)
{
    if(a==b)
    {
        e[c].l=e[c].r=a;
        e[c].w=1;
        e[c].to=0;
        return;
    }
    e[c].l=a;
    e[c].r=b;
    e[c].to=0;
    int mid=(a+b)/2;
    build(a,mid,2*c);
    build(mid+1,b,2*c+1);
    e[c].w=e[2*c].w+e[2*c+1].w;
}
void update(int a,int b,int c,int val)
{
    if(e[c].l==a&&e[c].r==b)
    {
        e[c].to=val;
        return;
    }
    int mid=(e[c].l+e[c].r)/2;
    if(e[c].to!=0)     //如果查找当前修改段时碰到其他修改段,则将其他修改段向下拓展
    {
        update(e[c].l,mid,2*c,e[c].to);
        update(mid+1,e[c].r,2*c+1,e[c].to);
        e[c].to=0;
    }
    if(b<=mid)
        update(a,b,2*c,val);
    else if(a>mid)
        update(a,b,2*c+1,val);
    else
    {
        update(a,mid,2*c,val);
        update(mid+1,b,2*c+1,val);
    }
}
int query(int a,int b,int c,int val)
{
    if(a==b)
    {
        if(val!=0)
            e[c].w=val;
        else if(e[c].to!=0)
            e[c].w=e[c].to;
        return e[c].w;
    }
    if(val!=0)
        e[c].to=val;
    else
        val=e[c].to;
    int mid=(e[c].l+e[c].r)/2;
    if(b<=mid)
        return query(a,b,2*c,val);
    else if(a>mid)
        return query(a,b,2*c+1,val);
    else
        return query(a,mid,2*c,val)+query(mid+1,b,2*c+1,val);
}
int main()
{
    int n,m;
    int T,tt=0;
    char s[11];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int i,j,k,a,b,c;
        build(1,n,1);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,1,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",++tt,query(1,n,1,0));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值