D. Welfare State 【Codeforces Round #576 (Div. 2) D】【线段树】

题目链接

题目大意

有n个数,m次操作:
1 x y 的意思是把第x个数改为y
2 x 的意思是把这n个数所有小于x的数都改为x

解题思路

先建一棵线段树,用add表示这个数应该不小于add,就和之前的lazy差不多,遇到就往下传,只不过要传最大的那个

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int N=2e5+5;
int a[N];
struct node
{
    int l,r,sum,add;
};
node e[N*4];

void build(int root,int l,int r)
{
    e[root].l=l;
    e[root].r=r;
    e[root].add=e[root].sum=0;
    if(l==r)
    {
        e[root].sum=a[l];
        return ;
    }
    build(root*2,l,(l+r)/2);
    build(root*2+1,(l+r)/2+1,r);
}
void updown(int root)
{
    if(e[root].add)
    {
        e[root*2].add=max(e[root*2].add,e[root].add);
        e[root*2+1].add=max(e[root*2+1].add,e[root].add);
        e[root].add=0;
    }
}
void update(int root,int x,int y)
{
    if(e[root].l==e[root].r)
    {
        e[root].add=0;
        e[root].sum=y;
        return ;
    }
    updown(root);
    int mid=(e[root].l+e[root].r)/2;
    if(x<=mid)
        update(root*2,x,y);
    else
        update(root*2+1,x,y);
}
int que(int root,int x)
{
    if(e[root].l==e[root].r)
    {
        return max(e[root].add,e[root].sum);
    }
    updown(root);
    int mid=(e[root].l+e[root].r)/2;
    if(x<=mid)
        return que(root*2,x);
    else
        return que(root*2+1,x);
}
int main()
{
    int n,m,x,y,z;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&z);
        if(z==1)
        {
            scanf("%d %d",&x,&y);
            update(1,x,y);
        }
        else
        {
            scanf("%d",&x);
            e[1].add=max(e[1].add,x);
        }
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d%c",que(1,i),i==n?'\n':' ');
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值