#include<string>
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int pos[maxn];
struct tree {
int l, r;
int val;
}t[maxn * 4];
int ans = 0;
void Build(int i, int l, int r) {
t[i].l = l;
t[i].r = r;
t[i].val = 0;
if (l == r) {
pos[l] = i; return;
}
int mid = l + r >> 1;
Build(i << 1, l, mid);
Build((i << 1) + 1, mid + 1, r);
}
void Update(int i) {
if (i == 1) return;
int root = i >> 1;
int x = t[root << 1].val;
int y = t[(root << 1) + 1].val;
t[root].val = max(x, y);
Update(root);
}
void Query(int i, int l, int r) {
if (l == t[i].l && r == t[i].r) {
ans = max(ans, t[i].val);
return;
}
i <<= 1;
if (l <= t[i].r) {
if (r <= t[i].r) Query(i, l, r);
else Query(i, l, t[i].r);
}
i++;
if (r >= t[i].l) {
if (l >= t[i].l) Query(i, l, r);
else Query(i, t[i].l, r);
}
}
int main() {
int n, m;
while (scanf("%d %d", &n, &m) != EOF) {
Build(1, 1, n);
for (int i = 1; i <= n; i++) {
int val; scanf("%d", &val);
t[pos[i]].val = val;
Update(pos[i]);
}
while (m--) {
string s;
cin >> s;
if (s[0] == 'Q') {
int l, r;
scanf("%d %d", &l, &r);
ans = 0;
Query(1, l, r);
printf("%d\n", ans);
}
else {
int i, val;
scanf("%d %d", &i, &val);
t[pos[i]].val = val;
Update(pos[i]);
}
}
}
return 0;
}
模板——线段树之最大值问题(单点修改,区间查询)
最新推荐文章于 2024-02-28 19:53:44 发布