HDU1754 I Hate It

来源:互联网 发布:淘宝c店还能做吗 编辑:程序博客网 时间:2024/05/16 17:30
/*线段树 单点更新 + 寻找区间最值*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 200000 + 5;struct node {int l, r, Max, score;};node tree[maxn * 4];int n, m, ans;void build(int l, int r, int root){tree[root].l = l;tree[root].r = r;if (l == r) {scanf("%d", &tree[root].score);tree[root].Max = tree[root].score;return;}int mid = (tree[root].l + tree[root].r) >> 1;build(l, mid, root << 1);build(mid + 1, r, root << 1 | 1);tree[root].Max = max(tree[root << 1].Max, tree[root << 1 | 1].Max);}void query(int l, int r, int root, int L, int R){if (L <= tree[root].l && R >= tree[root].r) {ans = max(ans, tree[root].Max);return;}int mid = (tree[root].l + tree[root].r) >> 1;if (R <= mid)query(l, mid, root << 1, L, R);else if (L > mid)query(mid  + 1 , r, root << 1 | 1, L, R);else {query(l, mid, root << 1, L, R);query(mid + 1, r, root << 1 | 1, L, R);}}void update(int l, int r, int root, int pos, int add){if (l == r) {tree[root].score = tree[root].Max = add;return;}int mid = (tree[root].l + tree[root].r) >> 1;if (pos <= mid)update(l, mid, root << 1, pos, add);elseupdate(mid + 1, r, root << 1 | 1, pos, add);tree[root].Max = max(tree[root << 1].Max, tree[root << 1 | 1].Max);}int main(){while (cin >> n >> m) {build(1, n, 1);int x, y;char s[10];for (int i = 0; i < m; ++i) {scanf("%s", s);scanf("%d%d", &x, &y);if (s[0] == 'Q') {ans = -1;query(1, n, 1, x, y);printf("%d\n", ans);}else {update(1, n, 1, x, y);}}}return 0;}

0 0
原创粉丝点击