HDU - 1754 I Hate It
阿新 • • 發佈:2021-01-13
題目連結: I Hate It
大致題意:
解題思路:
線段樹模板題,考察單點修改區間查詢,維護的是區間的最大值
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m;
int w[N];
struct node {
int l, r;
int maxx;
}t[N * 4];
void pushup(int index) {
t[index].maxx = max(t[index << 1].maxx, t[index << 1 | 1].maxx);
}
void build(int l, int r, int index = 1) {
if (l == r)t[index] = { l,r,w[l] };
else {
t[index] = { l,r };
int mid = l + r >> 1;
build(l, mid, index << 1); build(mid + 1, r, index << 1 | 1);
pushup(index);
}
}
int ask(int l, int r, int index = 1) {
if (t[index].l >= l && t[index].r <= r)return t[index].maxx;
else {
int mid = t[index].l + t[index].r >> 1;
int res = 0;
if (l <= mid)res = ask(l, r, index << 1);
if (r > mid)res = max(res, ask(l, r, index << 1 | 1));
return res;
}
}
void modify (int x, int v, int index = 1) {
if (t[index].l == x && t[index].r == x)t[index].maxx = v;
else {
int mid = t[index].l + t[index].r >> 1;
if (x <= mid)modify(x, v, index << 1);
if (x > mid)modify(x, v, index << 1 | 1);
pushup(index);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
while (cin >> n >> m) {
for (int i = 1; i <= n; ++i)cin >> w[i];
build(1, n);
while (m--) {
char op[2]; int l, r;
int x, v;
cin >> op;
if (*op == 'Q') {
cin >> l >> r;
cout << ask(l, r) << endl;
}
else {
cin >> x >> v;
modify(x, v);
}
}
}
return 0;
}