线段树
重点
1. 根据题目分析,节点需要添加的新的信息,新信息如何维护,继续思考,直到全部信息可知
2. query的时候,需要分情况,因为查询的东西比较特殊,需要利用返回值
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010, M = 100010;
int a[N];
int n, m;
struct Node {
int l, r;
int sum, lmax, rmax, tmax;
} tr[N * 4];
void pushup(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(max(l.tmax, r.tmax), l.rmax + r.lmax);
}
void pushup(int u) {
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r) {
if (l == r) tr[u] = {l, r, a[l], a[l], a[l], a[l]};
else {
tr[u] = {l, r};
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 == x && tr[u].r == x) tr[u] = {x, x, v, v, v, v};
else {
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];
else {
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
else if (l > mid) return query(u << 1 | 1, l, r);
else {
auto lnode = query(u << 1, l, r);
auto rnode = query(u << 1 | 1, l, r);
Node res;
pushup(res, lnode, rnode);
return res;
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
int x, y, k;
while (m--) {
scanf("%d%d%d", &k, &x, &y);
if (k == 1) {
if (x > y) swap(x, y);
auto t = query(1, x, y);
printf("%d\n", t.tmax);
} else {
modify(1, x, y);
}
}
return 0;
}