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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <iostream> #include <cstdio> using namespace std; const int N = 5e4 + 10; int n, m, w[N]; struct Node { int l, r, mx, sum, lmx, rmx; }tr[N << 2]; void pushup (int x) { tr[x].sum = tr[x << 1].sum + tr[x << 1 | 1].sum; tr[x].lmx = max(tr[x << 1].lmx, tr[x << 1].sum + tr[x << 1 | 1].lmx); tr[x].rmx = max(tr[x << 1 | 1].rmx, tr[x << 1].rmx + tr[x << 1 | 1].sum); tr[x].mx = max(tr[x << 1].rmx + tr[x << 1 | 1].lmx, max(tr[x << 1].mx, tr[x << 1 | 1].mx)); } void build (int x, int l, int r) { tr[x].l = l; tr[x].r = r; if (l == r) { tr[x].sum = tr[x].mx = tr[x].lmx = tr[x].rmx = w[l]; return; } int mid = l + r >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(x); } void modify (int x, int t, int k) { if (tr[x].l == tr[x].r) { tr[x].sum = tr[x].mx = tr[x].lmx = tr[x].rmx = k; return; } int mid = tr[x].l + tr[x].r >> 1; if (t <= mid) modify(x << 1, t, k); if (t > mid) modify(x << 1 | 1, t, k); pushup(x); } Node query (int x, int l, int r) { if (tr[x].l >= l && tr[x].r <= r) return tr[x]; int mid = tr[x].l + tr[x].r >> 1; if (l > mid) return query(x << 1 | 1, l, r); if (r <= mid) return query(x << 1, l, r); Node res, lres = query(x << 1, l, r), rres = query(x << 1 | 1, l, r); res.sum = lres.sum + rres.sum; res.lmx = max(lres.lmx, lres.sum + rres.lmx); res.rmx = max(rres.rmx, rres.sum + lres.rmx); res.mx = max(lres.rmx + rres.lmx, max(lres.mx, rres.mx)); return res; } int main () { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); build(1, 1, n); scanf("%d", &m); for (int op, x, y; m; m--) { scanf("%d%d%d", &op, &x, &y); if (op) printf("%d\n", query(1, x, y).mx); else modify(1, x, y); } return 0; }
|