hdu 1698 Just a Hook(线段树)

懒惰标记

#include <cstdio>

struct node
{
    int l,r,sum,lazy;
};

node segTree[400004];

void Build(int t, int l, int r)
{
    segTree[t].l = l;
    segTree[t].r = r;
    segTree[t].lazy = 0;
    if(l == r)
    {
        segTree[t].sum = 1;
        return ;
    }
    int mid = (l+r) >> 1;
    Build(t<<1,l,mid);
    Build(t<<1|1,mid+1,r);

    segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}

void pushDown(int t)
{
    if(segTree[t].lazy)
    {
        segTree[t<<1].sum = (segTree[t<<1].r - segTree[t<<1].l + 1) * segTree[t].lazy;
        segTree[t<<1].lazy = segTree[t].lazy;
        segTree[t<<1|1].sum = (segTree[t<<1|1].r - segTree[t<<1|1].l + 1)*segTree[t].lazy;
        segTree[t<<1|1].lazy = segTree[t].lazy;

        segTree[t].lazy = 0;
    }
}

void Update(int t, int l, int r, int c)
{
    if(segTree[t].l >= l && segTree[t].r <= r)
    {
        segTree[t].sum = (segTree[t].r - segTree[t].l + 1) * c;
        segTree[t].lazy = c;
        return ;
    }

    pushDown(t);

    if(r >= segTree[t<<1|1].l)
        Update(t<<1|1,l,r,c);
    if(l <= segTree[t<<1].r)
        Update(t<<1,l,r,c);

    segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}

int main()
{
    int t,n,o;
    int time = 0;
    scanf("%d",&t);
    while(t--)
    {
        ++time;
        scanf("%d",&n);
        Build(1,1,n);
        scanf("%d",&o);
        int a,b,c;
        for(int i = 0; i < o; ++i)
        {
            scanf("%d %d %d",&a,&b,&c);
            Update(1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",time,segTree[1].sum);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值