http://acm.hdu.edu.cn/showproblem.php?pid=1754
维护区间的最大值,使用线段树的单点更新。
#include <map>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 200000;
struct node{
int l,r;
int value;
int mid(){
return (l + r) >> 1;
}
}tree[MAX * 4];
void buildtree(int l, int r, int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l == r){
scanf("%d",&tree[rt].value);
return;
}
int mid = tree[rt].mid();
buildtree(l, mid, rt << 1);
buildtree(mid + 1, r, rt << 1 | 1);
tree[rt].value = max(tree[rt << 1].value, tree[rt << 1 | 1].value);
}
void update(int rt, int pos, int key){
if(tree[rt].l == pos && tree[rt].r == pos){
tree[rt].value = key;
return;
}
int mid = tree[rt].mid();
if(pos <= mid)
update(rt << 1, pos, key);
else update(rt << 1 | 1, pos, key);
tree[rt].value = max(tree[rt << 1].value, tree[rt << 1 | 1].value);
}
int query(int rt, int a, int b){
if(a <= tree[rt].l && b >= tree[rt].r)
return tree[rt].value;
int mid = tree[rt].mid();
if(b <= mid)
query(rt << 1, a, b);
else if(a > mid)
query(rt << 1 | 1 , a, b);
else
return max(query(rt << 1, a, mid), query(rt << 1 | 1 , mid + 1, b));
}
int main(){
// freopen("in.txt", "r", stdin);
int N,M;
while(scanf("%d%d",&N,&M) != EOF){
buildtree(1, N, 1);
char ch;
while(M--){
getchar();
scanf("%c",&ch);
int a,b;
scanf("%d%d",&a,&b);
if(ch == 'Q'){
int ans = query(1, a, b);
printf("%d\n",ans);
}
else update(1, a, b);
}
}
return 0;
}