原题
http://acm.hdu.edu.cn/showproblem.php?pid=1754
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <vector>
#include <math.h>
using namespace std;
#define lson l,m,rt<<1//左儿子
#define rson m+1,r,rt<<1|1//右儿子
const int maxn = 222222;
int MAX[maxn<<2];
int max(int a,int b){
if(a >= b)
return a;
else
return b;
}
void pushup(int rt){
MAX[rt] = max(MAX[rt<<1],MAX[rt<<1|1]);
}
void build(int l,int r,int rt){//建立线段树
if(l == r){//输入
scanf("%d",&MAX[rt]);
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){//如果要找的区间比现在的要大,那么可以返回现在区间的最大值
return MAX[rt];
}
int m = (l+r)>>1;
int ret = 0;
if(L <= m){//这步举个简单的例子,如果说我要找的区间是2到4,m等于3
ret = max(ret,query(L,R,lson));
}
if(R > m){
ret = max(ret,query(L,R,rson));
}
return ret;
}
void update(int a,int b,int l,int r,int rt){
if(l == r){
MAX[rt] = b;
return ;
}
int m = (l+r)>>1;
if(a <= m){//和要找的那个点进行比较。从而确定是更新左区间还是右区间
update(a,b,lson);
}
else{
update(a,b,rson);
}
pushup(rt);
}
int main(){
int n,m;
char op[2];
int a,b;
while(~scanf("%d%d",&n,&m)){
build(1,n,1);
while(m--){
scanf("%s%d%d",op,&a,&b);
if(op[0] == 'Q'){
printf("%d\n",query(a,b,1,n,1));
}
else{
update(a,b,1,n,1);
}
}
}
return 0;
}