题目:http://acm.hdu.edu.cn/showproblem.php?pid=1754
寻找区间最大值。
#include <iostream>
#include <cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 200005;
int tree[maxn<<2];
char str[10];
void PushUp(int rt){
tree[rt] = max(tree[rt<<1],tree[rt<<1|1]);
}
void build(int l, int r, int rt){
if (l == r){
scanf("%d",&tree[rt]);
return;
}
int m = (l + r) >> 1;
build (lson);
build (rson);
PushUp(rt);
}
void update(int id ,int p, int l, int r, int rt){
if (l == r){
tree[rt] = p;
return ;
}
int m = (l + r) >> 1;
if (id <= m)
update(id,p,lson);
else
update(id,p,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L <= l && r <= R){
return tree[rt];
}
int m = (l + r)>>1;
int ret = 0;
if(L <= m)
ret = max(ret,query(L, R, lson));
if(R > m)
ret = max(ret,query(L, R, rson));
return ret;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
build(1,n,1);
int a,b;
while(m--){
scanf("%s",str);
scanf("%d%d",&a,&b);
if(str[0] == 'Q')
printf("%d\n",query(a,b,1,n,1));
else if(str[0] == 'U')
update(a,b,1,n,1);
// for(int i = 1;i < n*4;i++)
// printf("%d ",tree[i]);
// printf("\n");
}
}
return 0;
}