D. Welfare State(递推/线段树)

D. Welfare State
time limit per test2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
There is a country with n citizens. The i-th of them initially has ai money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.

Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don’t send a receipt.

You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events.

Input
The first line contains a single integer n (1≤n≤2⋅105) — the numer of citizens.

The next line contains n integers a1, a2, …, an (0≤ai≤109) — the initial balances of citizens.

The next line contains a single integer q (1≤q≤2⋅105) — the number of events.

Each of the next q lines contains a single event. The events are given in chronological order.

Each event is described as either 1 p x (1≤p≤n, 0≤x≤109), or 2 x (0≤x≤109). In the first case we have a receipt that the balance of the p-th person becomes equal to x. In the second case we have a payoff with parameter x.

Output
Print n integers — the balances of all citizens after all events.

Examples
input Copy

4
1 2 3 4
3
2 3
1 2 2
2 1

output Copy

3 2 3 4 

input Copy

5
3 50 2 1 10
3
1 2 0
2 8
1 3 20

output Copy

8 8 20 8 10 

Note
In the first example the balances change as follows: 1 2 3 4 → 3 3 3 4 → 3 2 3 4 → 3 2 3 4

In the second example the balances change as follows: 3 50 2 1 10 → 3 0 2 1 10 → 8 8 8 8 10 → 8 8 20 8 10

大致题意:
当操作为1时,修改p位置上的数为x
操作为2时,整个区间,比x小的改为x
求经过q次操作后,整个序列的值是多少

Ac_code1:
打破直接套线段树 之后的 常规思维,递推~

/*
要记录:
修改过的元素: 最后一次修改后 2操作的最大值
没修改过:就是全体过程 2操作中的最大值
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int a[N],ma[N];
int last[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
    }
    int q;
    int maxx = 0;
    scanf("%d",&q);
    for(int i = 1; i <= q; i++)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op == 2)
        {
            ma[i] = x;
            maxx = max(maxx,x);
        }
        else
        {
            last[x] = i;  //第x号元素最后一次修改在第几次操作
            scanf("%d",&a[x]);
        }
    }
    //第i次操作后 2操作的最大值
    for(int i = q-1; i >= 1; i--)
    {
        ma[i] = max(ma[i+1],ma[i]);
    }
    for(int i = 1; i <= n; i++)
    {
        if(last[i])//i号元素修改过,最后一次修改后的最大值
            a[i] = max(a[i],ma[last[i]]);
        else //没修改过,整个过程的最大值
            a[i] = max(a[i],maxx);
        if(i < n)
            printf("%d ",a[i]);
        else
            printf("%d\n",a[i]);

    }
    return 0;
}

/*
5
1 2 3 4 5
5
2 5
1 1 3
2 4
1 2 3
2 2
*/

Ac_code2:
线段树

/*
线段树写法:
维护区间最小值,lazy tag记录区间更新的最大值
op = 1:单点更新,此时可以下传标记
op = 2:区间更新,实质上是在整个区间上更新,此时也打标记
ans:输出结果,每次单点查询,同时下传标记
*/
#include <bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int N = 2e5+5;
int a[N];
int minx[N<<2];
int tag[N<<2];

//向上更新
inline void push_up(int rt)
{
    minx[rt] = min(minx[rt<<1],minx[rt<<1|1]);
    return;
}
//建树
void build(int rt,int l,int r)
{
    tag[rt] = 0;
    if(l == r)
    {
        minx[rt] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    push_up(rt);
}
//标记下传
inline void push_down(int rt)
{
    if(tag[rt])
    {
        tag[rt<<1] = max(tag[rt<<1],tag[rt]);
        tag[rt<<1|1] = max(tag[rt<<1|1],tag[rt]);
        minx[rt<<1] = max(minx[rt<<1],tag[rt]);
        minx[rt<<1|1] = max(minx[rt<<1|1],tag[rt]);
        tag[rt] = 0;
    }
    return;
}
//单点更新
void update_point(int pos,int v,int rt,int l,int r)
{
    if(l == r)
    {
        minx[rt] = v;
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(pos <= mid) update_point(pos,v,lson);
    else update_point(pos,v,rson);
    push_up(rt);
}
//区间更新
void update_range(int L,int R,int v,int rt,int l,int r)
{
    if(l <= L && R <= r)
    {
        tag[rt] = max(tag[rt],v);
        minx[rt] = max(minx[rt],v);
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update_range(L,R,v,lson);
    if(R > mid) update_range(L,R,v,rson);
    push_up(rt);
}
//单点查询
int query(int pos,int rt,int l,int r)
{
    if(l == r)
    {
        return minx[rt];
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(pos <= mid) return query(pos,lson);
    else  return query(pos,rson);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
    }
    build(1,1,n);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int op,p,x;
        scanf("%d",&op);
        if(op == 1)
        {
            scanf("%d%d",&p,&x);
            update_point(p,x,1,1,n);
        }
        else
        {
            scanf("%d",&x);
            update_range(1,n,x,1,1,n);
        }
    }
    for(int i = 1; i < n; i++)
    {
        printf("%d ",query(i,1,1,n));
    }
    printf("%d\n",query(n,1,1,n));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leo Bliss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值