Codeforces Round #576 (Div. 1) B.Welfare State

      题目链接:Problem - B - Codeforces

        理解线段树的含义及其使用价值已经好久,比赛中也上别人板子改动过这个东西,在这个题目上,我实现了第一次同时满足以下条件:自己上手、没套任何板子、码出并且过题的线段树。虽然题不难,比较裸,但还是有一点小小的兴奋,冲淡了不少刚开学的丧吧,小小纪念一下。

代码:

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define MAXN 1000005
int n, q;
vector<int> a(n);
int t[4*MAXN];

inline void init(int pos, int l, int r)
{
    if(l==r) t[pos]=a[l];
    else
    {
        t[pos]=0;
        init(pos<<1, l, (l+r)>>1);
        init((pos<<1)+1, ((l+r)>>1)+1, r);
    }
}

inline void push_down(int pos)
{
    if(t[pos<<1]<t[pos]) t[pos<<1]=t[pos];
    if(t[(pos<<1)+1]<t[pos]) t[(pos<<1)+1]=t[pos];
    t[pos]=0;
//    cout<<pos<<" "<<t[pos]<<" "<<t[pos<<1]<<" "<<t[(pos<<1)+1]<<endl;
}

inline void change_single(int pos, int l, int r, int x, int y)
{
//    cout<<"here"<<pos<<" "<<l<<" "<<r<<" "<<endl
    if(l==r){
            if(l==x) t[pos]=y;
//            cout<<l<<" "<<t[pos]<<endl;
    }
    else
    {
        if(t[pos]) push_down(pos);
        if( ((l+r)>>1) <x) change_single((pos<<1)+1, ((l+r)>>1)+1, r, x, y);
        else change_single((pos<<1), l, ((l+r)>>1), x, y);
    }
}

inline void print_now(int pos, int l, int r)
{
    if(l==r) cout<<t[pos]<<" ";
    else
    {
        push_down(pos);
        print_now(pos<<1, l, (l+r)>>1);
        print_now((pos<<1)+1, ((l+r)>>1)+1, r);
    }
}

inline void op1()
{
    int x, y;
    cin>>x>>y;
    change_single(1, 1, n, x, y);
//    print_now(1, 1, n);
}

inline void op2()
{
    int y;
    cin>>y;
    t[1]=t[1]>y?t[1]:y;
}

int main()
{
    int x;
    std::ios::sync_with_stdio(false);
    cout.tie(0);
    cin>>n>>q;
    a.resize(n+1);
    for(int i=1; i<=n; i++) cin>>a[i];
    init(1, 1, n);
//    print_init(1, 1, n);
    while(q--)
    {
        cin>>x;
        if(x==1) op1();
        else op2();
    }
    print_now(1, 1, n);
    cout<<'\n'<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值