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;
    }
  • 相关阅读:
    ANSYS Workbench 16.0超级学习手册(附光盘)
    自控力(套装共2册)
    超实用记忆力训练法
    大愿法师“幸福人生”系列丛书——禅心商道
    高情商沟通术(写给所有深受社交困扰的人)
    青年职业形象管理手册(高等职业教育在线开放课程新形态一体化规划教材)
    1019. 括号匹配
    1026. 高精度除法
    1018. A+B Problem Revisited
    1015. 高精度乘法
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11277171.html
Copyright © 2011-2022 走看看