来源:HDU1754
水题,注意在里面输入这个东西
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int MAXN = 200000+100;
const int INF = 0x3f3f3f3f;
int tree[MAXN<<2];
int arr[MAXN];
int n,m;
char ch[20];
void pushup(int rt){
tree[rt] = max(tree[lson],tree[rson]);
}
void build(int rt,int l,int r){
if(l == r){
//cout<<"rt = "<<rt<<" tree[rt] = ";
scanf("%d",&tree[rt]);
//cout<<tree[rt]<<endl;
return ;
}
int mid = l+r>>1;
build(lson,l,mid);
build(rson,mid+1,r);
pushup(rt);
}
void update(int rt,int l,int r,int pos,int data){
if(l == r && l == pos){
tree[rt] = data;
return ;
}
int mid = l+r>>1;
if(pos<=mid) update(lson,l,mid,pos,data);
else if(pos>mid) update(rson,mid+1,r,pos,data);
pushup(rt);
}
int query(int rt,int l,int r,int left,int right){
//cout<<rt<<" "<<l<<" "<<r<<" "<<left<<" "<<right<<endl;
if(left<=l && r<=right){
return tree[rt];
}
int mid = l+r>>1;
int res =-INF;
if(left<=mid) res =max(res,query(lson,l,mid,left,right));
if(right>mid) res =max(res,query(rson,mid+1,r,left,right));
return res;
}
int main(){
int x,y;
while(scanf("%d%d",&n,&m)!=EOF){
build(1,1,n);
while(m--){
scanf("%s",ch);
if(ch[0] == 'Q'){
scanf("%d%d",&x,&y);
printf("%d\n",query(1,1,n,x,y));
}
else if(ch[0] == 'U'){
scanf("%d%d",&x,&y);
update(1,1,n,x,y);
}
}
}
return 0;
}