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
| #include <algorithm> #include <cstdio> class SegmentTree { private: struct Node { int value; Node *left, *right; int L, R; inline void Update() { if (this->left) { this->value = std::max(this->left->value, this->right->value); } } Node(const int &l, const int &r) { this->L = l, this->R = r; this->left = this->right = NULL; if (l == r) { scanf("%d", &this->value); } else { register int mid((l + r) >> 1); this->left = new Node(l, mid); this->right = new Node(mid + 1, r); this->Update(); } } ~Node() { if (this->left) { delete this->left; delete this->right; } } inline int Query(const int &l, const int &r) { if (this->L >= l && this->R <= r) { return this->value; } else { register int mid((this->L + this->R) >> 1), ret(-2147483648); if (l <= mid) ret = std::max(ret, this->left->Query(l, r)); if (mid < r) ret = std::max(ret, this->right->Query(l, r)); return ret; } } inline void Modify(const int &kIndex, const int &kDelta) { if (this->L == this->R) { this->value = kDelta; } else { register int mid((this->L + this->R) >> 1); if (kIndex <= mid) this->left->Modify(kIndex, kDelta); else this->right->Modify(kIndex, kDelta); this->Update(); } } } * root;
public: SegmentTree() { this->root = NULL; } inline void Clear() { if (this->root) { delete this->root; this->root = NULL; } } inline void Build(const int &l, const int &r) { this->root = new Node(l, r); } inline int Query(const int &l, const int &r) { return this->root->Query(l, r); } inline void Modify(const int &kIndex, const int &kDelta) { this->root->Modify(kIndex, kDelta); } } T; int n, m; char C; int A, B; int main(int argc, char const *argv[]) { while (~scanf("%d %d", &n, &m)) { T.Clear(); T.Build(1, n); while (m--) { scanf("\n%c %d %d", &C, &A, &B); switch (C) { case 'Q': { printf("%d\n", T.Query(A, B)); break; } case 'U': { T.Modify(A, B); break; } } } } return 0; }
|