• CF-576D Welfare State (线段树)


    题意:给出n个人,以及每个人所对应的工资信息 。 有q次寻问(1<= q<=2e5) {看到这个寻问量就估计到是用线段树}

    然后有两种操作: 1 a b 表示把 第 a个人的工资信息变为b ,     2 x 表示把所有工资小于 x 的人工资全部更新为 x (这样以来就更加容易看出这就是 区间最大值记录以及修改问题)

    所以我们就使用线段树来进行对应修改 ,最后在dfs打印出对应的每一个人的工资

    代码:

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <cmath>
    using namespace std;
    
    const int inf=0x3f3f3f3f;
    const int maxn=2e5+7;
    struct node
    {
        int val;
        int tag;
    }tree[maxn<<2];
    int n;
    int a[maxn];
    void push_down(int root)
    {
        if(tree[root].tag!=-1)
        {
            tree[root<<1].tag=max(tree[root<<1].tag,tree[root].tag);
            tree[root<<1|1].tag=max(tree[root<<1|1].tag,tree[root].tag);
            tree[root].tag=-1;
        }
    }
    void build(int l,int r,int root)
    {
        tree[root].tag=-1;
        if(l==r)
        {
            tree[root].val=a[l];
            return ;
        }
        int mid=(l+r)>>1;
        build(l,mid,root<<1);
        build(mid+1,r,root<<1|1); 
    }
    void updata(int root,int i,int v,int l=1,int r=n)
    {
        if(l==r)
        {
            tree[root].val=v;
            tree[root].tag=-1;
            return ;
        }
        push_down(root);
        int mid=(l+r)>>1;
        if(i<=mid)
        {
            updata(root<<1,i,v,l,mid);
        }
        else
        {
            updata(root<<1|1,i,v,mid+1,r);
        }
    }
    
    void print(int root,int l,int r)
    {
        if(l==r)
        {
            //de(tree[root].val);
            cout<<max(tree[root].val,tree[root].tag)<<" ";
            return ;
        }
        push_down(root);
        int mid=(l+r)>>1;
        print(root<<1,l,mid);
        print(root<<1|1,mid+1,r);
        
    }
    int main()
    {
         ios::sync_with_stdio(false);
        std::cin.tie(0);
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        build(1,n,1);
        int q;
        cin>>q;
        int op;
        int p,x;
        while(q--)
        {
            cin>>op;
            if(op==1)
            {
                cin>>p>>x;
                updata(1,p,x);
            }
            else
            {
                cin>>x;
                tree[1].tag=max(tree[1].tag,x);
            }
        }
        print(1,1,n);
        return 0;
    }
  • 相关阅读:
    MISC | ctfshow 31
    010editor 没有分块高亮显示了
    BUUCTF | [网鼎杯 2020 朱雀组]phpweb
    python2与python3共存后,如何使用
    kali2020 装不上docker
    php代码审计整理
    [MRCTF2020]Ezpop
    kali没有tcptraceroute如何安装
    [BUUCTF] 真的很杂
    【弱网测试】备份弱网测试相关数据
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11277171.html
Copyright © 2020-2023  润新知