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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <bits/stdc++.h>
#define ed '\n' #define fi first #define se second #define fore(i, l, r) for(int i = (int)(l); i <=(int)r; i++) #define out(x, k) cout << fixed << setprecision(k) << x << ed
using namespace std;
typedef pair<int, int> PII; typedef long long LL; typedef double db;
const int INF = 1e9; const int N = 5e5 + 10;
int n, m; int w[N]; struct Node{ int l, r, sum; int lv, rv; int v; }tr[4 * N];
void pushup(Node &u,Node &l,Node &r) { u.sum = l.sum + r.sum; u.lv = max(l.lv, l.sum + r.lv); u.rv = max(r.rv, r.sum + l.rv); u.v = max({l.v, r.v, l.rv + r.lv}); }
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, w[r], w[r], w[r], w[r]}; 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 left = query(u << 1, l, r); auto right = query(u << 1 | 1, l, r); Node ans; pushup(ans, left, right); return ans; } } } void solve() { cin >> n >> m; fore (i, 1, n) cin >> w[i]; build(1, 1, n); while (m--) { int k, x, y; cin >> k >> x >> y; if (k == 1) { if (x > y) swap(x, y); cout << query(1, x, y).v << ed; } else { modify(1, x, y); } } } int main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int _ = 1; while(_--) { solve(); } return 0; }
|