线段树:

 1 #include<cstdio>
 2 #define INF 987654321
 3 #define MAXN 200010
 4 int tree[MAXN << 2];
 5 inline int MAX(int x, int y) {
 6     return x > y ? x : y;
 7 }
 8 inline void PushUp(int rt) {
 9     tree[rt] = MAX(tree[rt << 1], tree[rt << 1 | 1]);
10 }
11 void Build(int L, int R, int rt) {
12     if (L == R)
13         scanf("%d", &tree[rt]);
14     else {
15         int mid = (L + R) >> 1;
16         Build(L, mid, rt << 1);
17         Build(mid + 1, R, rt << 1 | 1);
18         PushUp(rt);
19     }
20 }
21 void Update(int x, int val, int L, int R, int rt) {
22     if (L == R)
23         tree[rt] = val;
24     else {
25         int mid;
26         mid = (L + R) >> 1;
27         if (mid >= x)
28             Update(x, val, L, mid, rt << 1);
29         else
30             Update(x, val, mid + 1, R, rt << 1 | 1);
31         PushUp(rt);
32     }
33 }
34 int Query(int x, int y, int L, int R, int rt) {
35     if (x <= L && R <= y)
36         return tree[rt];
37     int ans, mid;
38     ans = -INF;
39     mid = (L + R) >> 1;
40     if (x <= mid)
41         ans = MAX(ans, Query(x, y, L, mid, rt << 1));
42     if (y > mid)
43         ans = MAX(ans, Query(x, y, mid + 1, R, rt << 1 | 1));
44     return ans;
45 }
46 int main() {
47     int n, m, x, y;
48     char ch;
49     while (~scanf("%d%d", &n, &m)) {
50         Build(1, n, 1);
51         while (m--) {
52             scanf(" %c%d%d", &ch, &x, &y);
53             if (ch == 'Q')
54                 printf("%d\n", Query(x, y, 1, n, 1));
55             else
56                 Update(x, y, 1, n, 1);
57         }
58     }
59     return 0;
60 }

 

Splay:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define INF 987654321
 4 #define MAXN 300000
 5 using namespace std;
 6 struct SplayTree {
 7     int root;
 8     int next[MAXN][2], key[MAXN], pre[MAXN], res[MAXN];
 9     inline void PushUp(int x) {
10         res[x] = max(res[next[x][0]], res[next[x][1]]);
11         res[x] = max(res[x], key[x]);
12     }
13     void NewNode(int val, int x) {
14         next[x][0] = next[x][1] = 0;
15         key[x] = res[x] = val;
16         pre[x] = x - 1;
17         next[x - 1][1] = x;
18     }
19     inline void Rotate(int x, int kind) {
20         int y, z;
21         y = pre[x];
22         z = pre[y];
23         next[y][!kind] = next[x][kind];
24         pre[next[x][kind]] = y;
25         next[z][next[z][1] == y] = x;
26         pre[x] = z;
27         next[x][kind] = y;
28         pre[y] = x;
29         PushUp(y);
30         PushUp(x);
31     }
32     void Splay(int x, int goal) {
33         if (x != goal) {
34             while (pre[x] != goal) {
35                 if (next[pre[x]][0] == x)
36                     Rotate(x, 1);
37                 else
38                     Rotate(x, 0);
39             }
40             if (!goal)
41                 root = x;
42         }
43     }
44     void Update(int x, int val) {
45         x++;
46         Splay(x, 0);
47         key[x] = val;
48         PushUp(x);
49     }
50     void Query(int x, int y) {
51         y += 2;
52         Splay(x, 0);
53         Splay(y, x);
54         printf("%d\n", res[next[y][0]]);
55     }
56 } tree;
57 int main() {
58     char ch;
59     int n, q;
60     int i, x, y;
61     while (~scanf("%d%d", &n, &q)) {
62         tree.NewNode(-INF, 1);
63         for (i = 0; i < n; i++) {
64             scanf("%d", &x);
65             tree.NewNode(x, i + 2);
66         }
67         tree.NewNode(-INF, n + 2);
68         for (i = n + 2; i; i--)
69             tree.PushUp(i);
70         tree.root = 1;
71         while (q--) {
72             scanf(" %c%d%d", &ch, &x, &y);
73             if (ch == 'Q')
74                 tree.Query(x, y);
75             else
76                 tree.Update(x, y);
77         }
78     }
79     return 0;
80 }
posted on 2012-08-13 01:11  DrunBee  阅读(718)  评论(0编辑  收藏  举报