HDU 1698 Just a Hook(线段树区间替换)

题目地址:HDU 1698

区间替换裸题。相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int MAXN=1e5+10;
int sum[MAXN<<3], lazy[MAXN<<3];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt, int m)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=lazy[rt]*(m-(m>>1));
        sum[rt<<1|1]=lazy[rt]*(m>>1);
        lazy[rt]=0;
    }
}
void build(int l, int r, int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int mid=l+r>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int ll, int rr, int x, int l, int r, int rt)
{
    if(ll<=l&&rr>=r)
    {
        lazy[rt]=x;
        sum[rt]=(r-l+1)*x;
        return ;
    }
    PushDown(rt, r-l+1);
    int mid=l+r>>1;
    if(ll<=mid) update(ll,rr,x,lson);
    if(rr>mid) update(ll,rr,x,rson);
    PushUp(rt);
}
int main()
{
    int n, i, a, b, c, t, q, num=0;
    scanf("%d",&t);
    while(t--)
    {
        num++;
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.
",num,sum[1]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/gcczhongduan/p/5279347.html