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