5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
也是很基础的一道线段树:5 6 5 9
#include<iostream> #include<cstdio> using namespace std; const int MAX = 200005; struct ss { int left, right, value; }segtree[MAX*4]; int N, M, x, y, ans; char qu; void build(int node, int l, int r) { segtree[node].left = l; segtree[node].right = r; if(l == r) { scanf("%d", &segtree[node].value); return; } build(node*2, l, (l+r)/2); build(node*2+1, (l+r)/2+1, r); segtree[node].value = max(segtree[node*2].value, segtree[node*2+1].value); } void query(int node, int l, int r) { if(segtree[node].left > r || segtree[node].right < l) return; if(l <= segtree[node].left && r >= segtree[node].right) { ans = max(ans, segtree[node].value); return; } query(node*2, l, r); query(node*2+1, l, r); } void change(int node, int pos, int v) { if(pos > segtree[node].right || pos < segtree[node].left) return; if(segtree[node].right == segtree[node].left) { segtree[node].value = v; return; } change(node*2, pos, v); change(node*2+1, pos, v); segtree[node].value = max(segtree[node*2].value, segtree[node*2+1].value); } int main() { while(scanf("%d %d", &N, &M) != EOF) { build(1, 1, N); while(M--) { cin >> qu; scanf("%d %d", &x, &y); if(qu == 'Q') { ans = 0; query(1, x, y); cout << ans << endl; } else change(1, x, y); } } return 0; }