线段树(单点更新) HDU 1754 I Hate It

 

题目传送门

 1 /*  2  线段树基本功能:区间最大值,修改某个值  3 */  4 #include <cstdio>  5 #include <cstring>  6 #include <algorithm>  7 #define lson l, m, rt << 1  8 #define rson m+1, r, rt << 1|1  9 10 const int MAX_N = 200000 + 10; 11 int sum[MAX_N<<2]; 12 int a[MAX_N<<2]; 13 14 void pushup(int rt) 15 { 16 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 17 } 18 19 void maxrt(int rt) 20 { 21 sum[rt] = std::max (sum[rt<<1], sum[rt<<1|1]); 22 } 23 24 void build(int l, int r, int rt) 25 { 26 if (l == r) 27  { 28 scanf ("%d", &sum[rt]); 29 return ; 30  } 31 int m = (l + r) >> 1; 32  build (lson); 33  build (rson); 34  maxrt (rt); 35 } 36 37 void update(int p, int up, int l, int r, int rt) 38 { 39 if (l == r) 40  { 41 sum[rt] = up; 42 return ; 43  } 44 int m = (l + r) >> 1; 45 if (p <= m) update (p, up, lson); 46 else update (p, up, rson); 47  maxrt (rt); 48 } 49 50 int query(int ql, int qr, int l, int r, int rt) 51 { 52 if (ql <= l && r <= qr) 53  { 54 return sum[rt]; 55  } 56 int m = (l + r) >> 1; 57 int ans = 0; 58 if (ql <= m) ans = std::max(ans, query (ql, qr, lson)); 59 if (qr > m) ans = std::max(ans, query (ql, qr, rson)); 60 61 return ans; 62 } 63 64 65 int main(void) //HDU 1754 I Hate It 66 { 67 //freopen ("inB.txt", "r", stdin); 68 int n, m, ans, b, c; 69 char ch[2]; 70 71 while (~scanf ("%d%d", &n, &m)) 72  { 73 build (1, n, 1); 74 while (m--) 75  { 76 scanf ("%s%d%d", &ch, &b, &c); 77 if (ch[0] == 'Q') 78  { 79 ans = query (b, c, 1, n, 1); 80 printf ("%d\n", ans); 81  } 82 if (ch[0] == 'U') 83  { 84 update (b, c, 1, n, 1); 85  } 86  } 87  } 88 89 return 0; 90 }

 

你可能感兴趣的:(HDU)