HDU - 1754 I Hate It
线段树,单点修改,区间最大值查询。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n,m;
#define mid ((l + r) >> 1)
#define ls (nod << 1)
#define rs (nod << 1 | 1)
#define lson ls,l,mid
#define rson rs,mid + 1, r
int mx[N << 2];
void pushup(int nod) { mx[nod] = max(mx[ls], mx[rs]); }
void build(int nod,int l,int r){
if(l == r) { scanf("%d",&mx[nod]); return ; }
build(lson); build(rson); pushup(nod);
}
void update(int nod,int l,int r,int pos,int v){
if(l == r) { mx[nod] = v; return ;}
if(pos <= mid) update(lson,pos,v); else update(rson,pos,v);
pushup(nod);
}
int query(int nod,int l,int r,int ll,int rr){
if(l > rr || r < ll) return -2000000000;
if(ll <= l && r <= rr) return mx[nod];
return max(query(lson,ll,rr), query(rson,ll,rr));
}
int main(){
while(scanf("%d%d",&n,&m) != EOF){
build(1,1,n);
while(m --){
char c;
scanf(" %c",&c);
if(c == 'Q'){
int x,y; scanf("%d%d",&x,&y);
if(x > y) swap(x,y);
printf("%d\n",query(1,1,n,x,y));
}
if(c == 'U'){
int x,y; scanf("%d%d",&x,&y);
update(1,1,n,x,y);
}
}
}
return 0;
}