1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <iostream> using namespace std; const int N = 5e5 + 10, INF = -5e8 - 10; int n, m, k, a, b; struct Node { int l, r; int lmax, rmax, sum, tmax; } tr[N << 2]; void pushup(Node &rt, Node &left, Node &right) { rt.tmax = max(left.rmax + right.lmax, max(left.tmax, right.tmax)); rt.lmax = max(left.lmax, left.sum + right.lmax); rt.rmax = max(right.rmax, left.rmax + right.sum); rt.sum = left.sum + right.sum; } void pushup(int u) { auto &rt = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1]; pushup(rt, left, right); } void build(int u, int l, int r) { tr[u] = {l, r}; if (l == r) { scanf("%d", &a); tr[u].lmax = tr[u].rmax = tr[u].sum = tr[u].tmax = a; return; } int mid = l + r >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); pushup(u); } Node query(int u, int l, int r) { if (l <= tr[u].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 ans, ln = query(u << 1, l, r), rn = query(u << 1 | 1, l, r); pushup(ans, ln, rn); return ans; } void update(int u, int i, int v) { if (tr[u].l == i && tr[u].r == i) { tr[u].lmax = tr[u].rmax = tr[u].sum = tr[u].tmax = v; return; } int mid = tr[u].l + tr[u].r >> 1; if (i <= mid) update(u << 1, i, v); else update(u << 1 | 1, i, v); pushup(u); } int main() { scanf("%d%d", &n, &m); build(1, 1, n); while (m--) { scanf("%d%d%d", &k, &a, &b); if (k == 1) { if (a > b) swap(a, b); printf("%d\n", query(1, a, b).tmax); } else { update(1, a, b); } } return 0; }
|