hdu 1698 线段树 成段更新 lazy

学了一下午,终于学会了成段更新,以及lazy标记

#include<cstdio>
#include<iostream>
using namespace std;
#define lson id<<1
#define rson id<<1|1
const int Max = 500005;
struct line
{
    int left,right,mark;
    int sum;
    int mid()
    {
        return (left + right) / 2;
    }
}node[Max];
int a[Max];
void PushUp(int id)
{
    node[id].sum = node[lson].sum + node[rson].sum;
}
void Pushdown(int id,int c)
{
    if(node[id].mark>0)
    {
        node[lson].mark = node[rson].mark = node[id].mark;
        node[lson].sum = (node[lson].right-node[lson].left + 1) * node[lson].mark;
        node[rson].sum = (node[rson].right-node[rson].left + 1) * node[rson].mark;
        node[id].mark = 0;
    }
}
void _Build_Tree(int id,int l,int r)
{
    node[id].left = l,node[id].right = r;
    node[id].sum = node[id].mark = 1;
    if(l==r)return;
    int mid = node[id].mid();
    _Build_Tree(lson,l,mid);
    _Build_Tree(rson,mid+1,r);
    PushUp(id);
}
int n;
void upside(int l,int r,int c,int id)
{
    if(node[id].mark == c)return;
    if(node[id].left == l && node[id].right ==r)
    {
        node[id].mark = c;
        node[id].sum = (r - l + 1) * c;
        return ;
    }
    int mid = node[id].mid();
    Pushdown(id,c);
    if(l > mid)upside(l,r,c,rson);
    else if(r<=mid)upside(l,r,c,lson);
    else {
        upside(l,mid,c,lson);
        upside(mid+1,r,c,rson);
    }
    PushUp(id);
}
int main()
{
    int n,m;
    int T,l,r,c,cn = 0;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%d%d",&n,&m);
        _Build_Tree(1,1,n);

        while(m--)
        {
            scanf("%d %d %d",&l,&r,&c);
            upside(l,r,c,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",++cn,node[1].sum);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值