1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <bits/stdc++.h> #define ll long long #define IOS {ios::sync_with_stdio(false);cin.tie(0);} using namespace std;
const int maxn = 200000 + 5; ll N, M, a[maxn], d[maxn << 2]; int _a, _b; char ch; void push_up(int p) { d[p] = max(d[p << 1], d[p << 1 |1]); } void build(int l, int r, int p) { if(l == r) d[p] = a[l]; else { int m = (l + r) >> 1; build(l, m, p << 1); build(m + 1, r, p << 1 | 1); push_up(p); } } void update(int x, int v, int l, int r, int p) { if(l == r) d[p] = v; else { int m = (l + r) >> 1; if(x <= m) update(x, v, l, m, p << 1); else update(x, v, m + 1, r, p << 1 | 1); push_up(p); } } int query(int L, int R, int l, int r, int p) { if(L <= l && R >= r) return d[p]; int ans = 0; int m = (l + r) >> 1; if(L <= m) { ans = max(ans, query(L, R, l, m, p << 1)); } if(R > m) { ans = max(ans, query(L, R, m + 1, r, p << 1 | 1)); } return ans; } int main(void) { IOS while(cin >> N >> M) { memset(a, 0, sizeof(a)); memset(d, 0, sizeof(d)); for(int i = 1; i <= N; i++) { cin >> a[i]; } build(1, N, 1); while(M--) { cin >> ch; if(ch == 'Q') { cin >> _a >> _b; cout << query(_a, _b, 1, N, 1) << endl; } else if(ch == 'U') { cin >> _a >> _b; update(_a, _b, 1, N, 1); } } }
return 0; }
|