首页 > 代码库 > 【HDOJ】1754 I Hate It

【HDOJ】1754 I Hate It

线段树。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 #define mymax(a, b) (a>b) ? a:b
 7 
 8 const int maxn = 200005;
 9 
10 int nums[maxn<<2];
11 
12 void PushUP(int rt) {
13     nums[rt] = mymax(nums[rt<<1], nums[rt<<1|1]);
14 }
15 
16 void build(int l, int r, int rt) {
17     if (l == r) {
18         scanf("%d", &nums[rt]);
19         return ;
20     }
21     int mid = (l+r)>>1;
22     build(l, mid, rt<<1);
23     build(mid+1, r, rt<<1|1);
24     PushUP(rt);
25 }
26 
27 int query(int ll, int rr, int l, int r, int rt) {
28     if (ll <= l && rr>= r)
29         return nums[rt];
30     int mid = (l+r)>>1;
31     int max = -1, tmp = -1;
32     if (ll <= mid) {
33         max = query(ll, rr, l, mid, rt<<1);
34     }
35     if (rr > mid) {
36         tmp = query(ll, rr, mid+1, r, rt<<1|1);
37         if (tmp > max)
38             max = tmp;
39     }
40     return max;
41 }
42 
43 void update(int des, int val, int l, int r, int rt) {
44     if (l == r) {
45         nums[rt] = val;
46         return ;
47     }
48     int mid = (l+r)>>1;
49     if (des <= mid)
50         update(des, val, l, mid, rt<<1);
51     else
52         update(des, val, mid+1, r, rt<<1|1);
53     PushUP(rt);
54 }
55 
56 int main() {
57     int n, m;
58     int i, j;
59     char cmd[3];
60 
61     while (scanf("%d %d", &n, &m) != EOF) {
62         build(1, n, 1);
63         while (m--) {
64             scanf("%*c%s %d %d", cmd, &i, &j);
65             if (cmd[0] == Q)
66                 printf("%d\n", query(i,j,1,n,1));
67             else
68                 update(i,j,1,n,1);
69         }
70     }
71 
72     return 0;
73 }