题目链接
树状数组最值查询
//树状数组求最大值
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int a[N],c[2*N],n;
int lowbit(int x){
return x&(-x);
}
void add(int x){
while(x<=n){
c[x]=a[x];
for(int i=1;i<lowbit(x);i<<=1)
c[x]=max(c[x],c[x-i]);
x+=lowbit(x);
}
}
int getmax(int x,int y){
int ret=0;
while(y>=x){
ret=max(ret,a[y]);
y--;
for(;y-lowbit(y)>=x;y-=lowbit(y))
ret=max(ret,c[y]);
}
return ret;
}
int main(){
//freopen("in.txt","r",stdin);
int i,m,x,y;
char s;
while(scanf("%d%d",&n,&m)==2){
memset(c,0,sizeof(c));
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
add(i);
}
for(i=1;i<=m;i++){
s=getchar();
while(s!='Q'&&s!='U')s=getchar();//此处忘写
if(s=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",getmax(x,y));
}
else{
scanf("%d%d",&x,&y);
a[x]=y;
add(x);
}
}
}
return 0;
}