求区间最大值,利用线段树保存每个区间最大值,每次修改,更新由该点到根节点的区间最大值
#include
#include
using namespace std;
#define MAXN 200005
int ans;
struct node{
int left,right,maxx;
int mid(){
return (left+right)/2;
}
}tr[MAXN*4];
void buildTree(int l,int r,int i){
tr[i].left = l;
tr[i].right = r;
if(l==r){
scanf("%d",&tr[i].maxx);
return ;
}
int mid = tr[i].mid();
buildTree(l,mid,i<<1);
buildTree(mid+1,r,i<<1|1);
tr[i].maxx = max(tr[i<<1].maxx,tr[i<<1|1].maxx);
}
void query(int L,int R,int i,int l,int r){
if(l<=L&&r>=R){
ans = max(ans,tr[i].maxx);
return;
}
int mid = tr[i].mid();
if(r<=mid) query(L,mid,i<<1,l,r);
else if(l>mid) query(mid+1,R,i<<1|1,l,r);
else{
query(L,mid,i<<1,l,r);
query(mid+1,R,i<<1|1,l,r);
}
}
void update(int l,int r,int i,int pos,int val){
if(l==r){
tr[i].maxx = val;
return;
}
int mid = tr[i].mid();
if(pos<=mid) update(l,mid,i<<1,pos,val);
else update(mid+1,r,i<<1|1,pos,val);
tr[i].maxx = max(tr[i<<1].maxx,tr[i<<1|1].maxx);
}
int main(){
int n,m;
char c;
int a,b;
while(~scanf("%d %d",&n,&m)){
buildTree(1,n,1);
while(m--){
getchar();
c = getchar();
scanf("%d%d",&a,&b);
if(c=='Q'){
ans = -1;
query(1,n,1,a,b);
printf("%d\n",ans);
}
else{
update(1,n,1,a,b);
}
}
}
return 0;
}