# 14232: 【原4232】I Hate It

### 题目描述

author: Ge Yan 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4232

## Sample Input

``````5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
``````

## Sample Output

``````5
6
5
9
``````

## ligongzzz's solution

``````#include "iostream"
#include "cmath"
#include "cstdio"
#include "cstring"
using namespace std;

class node {
public:
int val = 0;
int left = 0, right = 0;
};

int mmax(int a, int b) {
return a > b ? a : b;
}

node smt[800000];
int val_data[200009] = { 0 };

void build_tree(int l, int r, int pos) {
smt[pos].left = l;
smt[pos].right = r;
if (l == r - 1) {
smt[pos].val = val_data[l];
return;
}

int mid = (l + r) >> 1;
build_tree(l, mid, pos * 2);
build_tree(mid, r, pos * 2 + 1);

smt[pos].val = mmax(smt[pos * 2].val, smt[pos * 2 + 1].val);
}

void update(int val, int pos, int root) {
if (smt[root].left == smt[root].right - 1) {
smt[root].val = val;
return;
}
int mid = (smt[root].left + smt[root].right) >> 1;
if (pos < mid)
update(val, pos, root * 2);
else
update(val, pos, root * 2 + 1);
smt[root].val = mmax(smt[root * 2].val, smt[root * 2 + 1].val);
}

int query(int l, int r, int root) {
if (l <= smt[root].left && smt[root].right <= r) {
return smt[root].val;
}
int mid = (smt[root].left + smt[root].right) >> 1;
int ans1 = 0, ans2 = 0;
if (l < mid) {
ans1 = query(l, r, root * 2);
}
if (r > mid) {
ans2 = query(l, r, root * 2 + 1);
}
return ans1 > ans2 ? ans1 : ans2;
}

int main() {
int n, m;
while (cin >> n >> m) {
memset(smt, 0, sizeof(smt));
memset(val_data, 0, sizeof(val_data));

for (int i = 0; i < n; ++i) {
scanf("%d", &val_data[i]);
}
build_tree(0, n, 1);
for (; m > 0; --m) {
char op;
int a, b;
scanf("\n%c %d %d", &op, &a, &b);
if (op == 'Q') {
printf("%d\n", query(a - 1, b, 1));
}
else {
update(b, a - 1, 1);
}
}
}

return 0;
}
``````

## q4x3's solution

``````/**
* 单点修改 + 区间最值
* 标准线段树板子
* 作为老师请务必严厉，不要让学生的成绩太好看
**/
#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 2e5 + 10;
int a[N], tree[N << 2];

void build(int rt, int l, int r) {
if(l == r) {
tree[rt] = a[l];
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
tree[rt] = (tree[rt << 1] > tree[rt << 1 | 1]) ? tree[rt << 1] : tree[rt << 1 | 1];
}

void modify(int rt, int l, int r, int x, int y) {
if(l == r) {
tree[rt] = y;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(rt << 1, l, mid, x, y);
else modify(rt << 1 | 1, mid + 1, r, x, y);
tree[rt] = (tree[rt << 1] > tree[rt << 1 | 1]) ? tree[rt << 1] : tree[rt << 1 | 1];
}

int query(int rt, int l, int r, int s, int t) {
if(s <= l && t >= r) return tree[rt];
int mid = (l + r) >> 1;
if(t <= mid) return query(rt << 1, l, mid, s, t);
else if(s > mid) return query(rt << 1 | 1, mid + 1, r, s, t);
else {
int tmp3 = query(rt << 1, l, mid, s, t);
int tmp4 = query(rt << 1 | 1, mid + 1, r, s, t);
return (tmp3 > tmp4) ? tmp3 : tmp4;
}
}

int n, m;

int main() {
scanf("%d", &n);
scanf("%d", &m);
for(register int i = 1;i <= n;++ i) {
scanf("%d", &a[i]);
}
build(1, 1, n);
for(register int i = 0;i < m;++ i) {
char tmp[3];
int tmp1, tmp2;
scanf("%s", tmp);
scanf("%d%d", &tmp1, &tmp2);
if(tmp[0] == 'Q') {
printf("%d\n", query(1, 1, n, tmp1, tmp2));
} else {
modify(1, 1, n, tmp1, tmp2);
}
}
}
``````

## victrid's solution

``````#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int seq[800005] = {};
struct set {
int l;
int r;
int current;
int& value;
set left() {
return set{l, (l + r) >> 1, current << 1, seq[current << 1]};
}
set right() {
return set{((l + r) >> 1) + 1, r, current << 1 | 1, seq[current << 1 | 1]};
}
bool isleaf() { return !(r - l); }
};
void buildtree(set s) {
if (s.isleaf()) {
scanf("%d", &(s.value));
return;
}
buildtree(s.left());
buildtree(s.right());
s.value = max(s.left().value, s.right().value);
return;
}
int query(int l, int r, set s) {
if (l <= s.l && s.r <= r) {
return s.value;
}
int mid = (s.l + s.r) >> 1;
int q1 = -1000, q2 = -1000;
if (l <= mid) {
q1 = query(l, r, s.left());
}
if (r > mid) {
q2 = query(l, r, s.right());
}
return max(q1, q2);
}
void update(int l, int r, set s) {
if (l < s.l || s.r < l) {
return;
}
if (s.isleaf()) {
s.value = r;
return;
}
int mid = (s.l + s.r) >> 1;
if (l <= mid) {
update(l, r, s.left());
} else {
update(l, r, s.right());
}
s.value = max(s.left().value, s.right().value);
return;
}
int main() {
//one group
int N, M;
char ch;
int l, r;
while (cin >> N >> M) {
buildtree(set{1, N, 1, seq[1]});
while (M--) {
scanf("\n%c %d %d", &ch, &l, &r);
switch (ch) {
case 'Q':
printf("%d\n", query(l, r, set{1, N, 1, seq[1]}));
break;
case 'U':
update(l, r, set{1, N, 1, seq[1]});
}
}
}
return 0;
}
``````