# GSS3 - Can you answer these queries III

## $$GSS3$$ - $$Can$$ $$you$$ $$answer$$ $$these$$ $$queries$$ $$III$$

SPOJ

$$GSS1$$对比，只是增加了一个单点修改。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
const int N = 500010;

int n, m;
int a[N];

struct Node {
int l, r;
int sum, lmax, rmax, tmax;
} tr[N << 2];

void calc(Node &u, Node &l, Node &r) {
u.sum = l.sum + r.sum;
u.lmax = max(l.lmax, l.sum + r.lmax);
u.rmax = max(r.rmax, r.sum + l.rmax);
u.tmax = max({l.tmax, r.tmax, l.rmax + r.lmax});
}

void pushup(int u) {
calc(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r) {
tr[u] = {l, r, 0, 0, 0, 0}; // 无初始值
if (l == r) {
tr[u].sum = tr[u].lmax = tr[u].rmax = tr[u].tmax = a[l];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}

void modify(int u, int x, int v) {
if (tr[u].l == tr[u].r) {
tr[u] = {x, x, v, v, v, v};
return;
}

int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid)
modify(u << 1, x, v);
else
modify(u << 1 | 1, x, v);
pushup(u);
}

Node query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u];

int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
if (l > mid) return query(u << 1 | 1, l, r);

Node a = query(u << 1, l, r), b = query(u << 1 | 1, l, r), res;
calc(res, a, b);
return res;
}

int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];

build(1, 1, n);

int op, x, y;
cin >> m;
while (m--) {
cin >> op >> x >> y;
if (op == 1) {
if (x > y) swap(x, y);
printf("%d\n", query(1, x, y).tmax);
} else
modify(1, x, y);
}
return 0;
}

posted @ 2022-04-28 22:08  糖豆爸爸  阅读(28)  评论(0编辑  收藏  举报
Live2D