1 #include
2 #include
3 #include
4 #define max(a, b) a>b?a:b
5 using namespace std;
6 const int MAXN = 200020;
7 struct Node
8 {
9 int max, left, right;
10 } tree[MAXN * 3];
11 int n, m, num[MAXN];
12 int Build(int root, int left, int right)
13 {
14 int mid;
15 tree[root].left = left;
16 tree[root].right = right;
17 if(left == right) // 叶子;
18 return tree[root].max = num[left];
19 mid = (left + right) / 2;
20 int a, b;
21 a = Build(2*root, left, mid);
22 b = Build(2*root + 1, mid + 1, right);
23 return tree[root].max = max(a, b);
24 }
25 int Update(int root, int pos, int val)
26 {
27 if(pos //该点不在子段中
28 return tree[root].max;
29 if(pos == tree[root].left && pos == tree[root].right) //叶子;
30 return tree[root].max = val;
31 int a, b;
32 a = Update(2*root, pos, val);
33 b = Update(2*root+1, pos, val);
34 tree[root].max = max(a, b);
35 return tree[root].max;
36 }
37 int Find(int root, int left, int right)
38 {
39 int mid;
40 if(tree[root].left > right || tree[root].right //没有相交线段;
41 return 0;
42 if(tree[root].left >= left && tree[root].right <= right) // 有一段连续的线段包含在查找区间内;
43 return tree[root].max;
44 int a, b;
45 a = Find(root*2, left, right);
46 b = Find(root*2 + 1, left, right);
47 return max(a, b);
48 }
49 int main()
50 {
51 while(~scanf("%d %d", &n, &m))
52 {
53 for(int i = 1; i <= n; i++)
54 scanf("%d", &num[i]);
55 Build(1, 1, n);
56 char str[10]; int a, b;
57 for(int i = 1; i <= m; i++)
58 {
59 cin >> str >> a >> b;
60 if(str[0] == ‘Q‘)
61 printf("%d\n", Find(1, a, b));
62 else
63 {
64 num[a] = b;
65 Update(1, a, b);
66 }
67 }
68 }
69 return 0;
70 }
1 http://blog.csdn.net/metalseed/article/details/8039326#
大神链接