简单的线段树,求区间最大值和修改某个点的值。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 200100
#define ls (p<<1)
#define rs (p<<1|1)
#define mid(p) (t[p].l+t[p].r>>1)
int v[N];
struct segmenttree{
int l,r,ma;
}t[N*4];
int max(int a,int b){return a>b?a:b;}
void build(int l,int r,int p){
t[p].l=l;t[p].r=r;
if (t[p].l==t[p].r){
t[p].ma=v[l];
return;
}
int m=mid(p);
build(l,m,ls);
build(m+1,r,rs);
t[p].ma=max(t[ls].ma,t[rs].ma);
}
int query(int l,int r,int p){
if (l==t[p].l&&r==t[p].r){
return t[p].ma;
}
int m=mid(p);
if (r<=m) {
return query(l,r,ls);
}
else if (l>m){
return query(l,r,rs);
}
else {
return max(query(l,m,ls),query(m+1,r,rs));
}
}
void update(int pos,int p,int nv){
if (t[p].l==t[p].r){
t[p].ma=nv;
return;
}
int m=mid(p);
if (pos<=m) {
update(pos,ls,nv);
}
else {
update(pos,rs,nv);
}
t[p].ma=max(t[ls].ma,t[rs].ma);
}
int main(){
int i;
int n,m,a,b;
char s[20];
while(scanf("%d%d",&n,&m)!=EOF){
for(i=1;i<=n;++i){
scanf("%d",&v[i]);
}
build(1,n,1);
for(i=1;i<=m;++i){
scanf("%s%d%d",s,&a,&b);
if (s[0]=='Q') {
printf("%d\n",query(a,b,1));
}
else {
update(a,1,b);
}
}
}
return 0;
}