题解
线段树,节点维护的是该节点管辖区间的最大值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define FIN freopen("data.in", "r", stdin);
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define Avg(x, y) ((x & y) + ((x ^ y) >> 1))
#define mem(a, v) memset(a,v,sizeof(a))
typedef long long LL;
const int maxn = 200000 + 3;
int tree[maxn * 3];
int n, m;
void build(int l, int r, int rt){
if(l == r){
scanf("%d", &tree[rt]);
return;
}
int m = Avg(l, r);
build(lson);
build(rson);
tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
}
void update(int l, int r, int rt, int id, int val){
if(l == id && id == r){
tree[rt] = val;
return;
}
int m = Avg(l, r);
if(id <= m) update(l, m, rt<<1, id, val);
else update(m+1, r, rt<<1|1, id, val);
tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
}
int query(int l, int r, int rt, int L, int R){
if(L <= l && r <= R) return tree[rt];
int m = Avg(l, r), ret;
if(R <= m) ret = query(l, m, rt<<1, L, R);
else if(L > m) ret = query(m+1, r, rt<<1|1, L, R);
else ret = max(query(l, m, rt<<1, L, R), query(m+1, r, rt<<1|1, L, R));
return ret;
}
int main(){
#ifdef EXMY
FIN
#endif // EXMY
// 1029MS 3916K
while(scanf("%d %d", &n, &m) != EOF){
mem(tree, 0);
build(1, n, 1);
char op;
int x, y;
getchar();
while(m--){
scanf("%c %d %d ", &op, &x, &y);
if(op == 'U') update(1, n, 1, x, y);
else printf("%d\n", query(1, n, 1, x, y));
}
}
return 0;
}