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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include <iostream> #include <cstdio> #include <cstring> #include <cctype> using namespace std;
const int MAXN = 200000 + 10;
int a[MAXN], segt[MAXN * 4]; int n, m;
inline int leftChild(int x) { return x << 1; }
inline int rightChild(int x) { return x << 1 | 1; }
inline void pushUp(int root) { segt[root] = std::max(segt[leftChild(root)], segt[rightChild(root)]); }
inline void buildTree(int l, int r, int root) { if (l == r) { segt[root] = a[l]; return; } int mid = (l + r) >> 1; buildTree(l, mid, leftChild(root)); buildTree(mid + 1, r, rightChild(root)); pushUp(root); }
inline int query(int l, int r, int ql, int qr, int root) { int res = -2147483640; if (ql <= l && r <= qr) return segt[root]; int mid = (l + r) >> 1; if (ql <= mid) res = std::max(res, query(l, mid, ql, qr, leftChild(root))); if (mid < qr) res = std::max(res, query(mid + 1, r, ql, qr, rightChild(root))); return res; }
inline void Modify(int l, int r, int dest, int root, int k) { if (l == r) { segt[root] = std::max(segt[root], k); return; } int mid = (l + r) >> 1; if (dest <= mid) Modify(l, mid, dest, leftChild(root), k); if (mid < dest) Modify(mid + 1, r, dest, rightChild(root), k); pushUp(root); }
inline int getint() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
inline void putint(int x, bool returnValue) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) putint(x / 10, false); putchar(x % 10 + '0'); if (returnValue) putchar('\n'); }
int main(int argc, char *const argv[]) { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; buildTree(1, n, 1); for (int i = 1; i <= m; ++i) { char op; int a, b; cin >> op >> a >> b; switch(op) { case 'Q': { putint(query(1, n, a, b, 1), true); break; } case 'U': { Modify(1, n, a, 1, b); break; } } } return 0; }
|