zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    代码重构~方法归子
    代码重构~代码注释
    VS2010安装帮助文档
    imagecreate()与imagecreatetruecolor()区别
    php cookie 和session详解
    horner's rule霍纳法则及综合除法
    php单引号和双引号的区别与用法
    php通过session判断用户是否登录
    php 产生验证码
    基于递归的整数幂的计算
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11277171.html
Copyright © 2011-2022 走看看