线段树(单点更新) 之 hdu 1754

//  [7/25/2014 Sjm]
/*
线段树单点更新水题。。。
*/
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 #define lson l, m, rt<<1
 7 #define rson m+1, r, rt<<1|1
 8 #define GetMid l + ((r-l)>>1)
 9 
10 const int MAX_N = 200005;
11 int MAX[MAX_N << 2];
12 
13 void PushUp(int rt) { MAX[rt] = max(MAX[rt << 1], MAX[rt << 1 | 1]); }
14 
15 void Build(int l, int r, int rt) {
16     if (l == r) { scanf("%d", &MAX[rt]); return; }
17     int m = GetMid;
18     Build(lson);
19     Build(rson);
20     PushUp(rt);
21 }
22 
23 void Update(int p, int grade, int l, int r, int rt) {
24     if (l == r) { MAX[rt] = grade; return; }
25     int m = GetMid;
26     if (p <= m) { Update(p, grade, lson); }
27     else { Update(p, grade, rson); }
28     PushUp(rt);
29 }
30 int Query(int L, int R, int l, int r, int rt) {
31     if (L <= l && r <= R) { return MAX[rt]; }
32     int m = GetMid;
33     int ans = 0;
34     if (L <= m) { ans = max(ans, Query(L, R, lson)); }
35     if (R > m) { ans = max(ans, Query(L, R, rson)); }
36     return ans;
37 }
38 
39 int main()
40 {
41     //freopen("input.txt", "r", stdin);
42     //freopen("output.txt", "w", stdout);
43     int N, M, a, b;
44     char ch[2];
45     while (~scanf("%d %d", &N, &M)) {
46         Build(1, N, 1);
47         while (M--) {
48             scanf("%s %d %d", ch, &a, &b);
49             if ('Q' == ch[0]) { printf("%d
", Query(a, b, 1, N, 1)); }
50             else  { Update(a, b, 1, N, 1); }
51         }
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/shijianming/p/4140818.html