• 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;
    }
    


  • 相关阅读:
    Java Stream 去重对象
    CentOS7安装JDK8
    CentOS7安装Tomcat9
    MySQL 8.0 安装
    面相对象7大原则
    Spring Boot MyBatis连接MySQL数据库
    win11右键改回win10风格,win10右键改回win11风格,不用重启
    【转载】NetCore 开发实战(目录整理)
    viewState cookie session _VIEWSTATE
    外部JS得到客户端ID
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5279347.html
Copyright © 2020-2023  润新知