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
| #include <bits/stdc++.h> #define int long long using namespace std;
const int N = 5e5+66;
int n, m, ans; int a[N], d[N<<6], zuo[N<<6], you[N<<6], duo[N<<6],tot;
inline void build (int s, int t, int p) { if (s == t) { d[p] = zuo[p] = you[p] = duo[p] = a[s]; return; } int m = (s+t)>>1; build (s, m, p<<1), build (m+1, t, (p<<1)|1); d[p] = (d[p<<1] + d[(p<<1)|1]); zuo[p] = max (zuo[p<<1], d[p<<1]+zuo[(p<<1)|1]); you[p] = max (you[(p<<1)|1], d[(p<<1)|1]+you[p<<1]); duo[p] = max (max(duo[p<<1], duo[(p<<1)|1]), you[p<<1]+zuo[(p<<1)|1]); }
inline void chenge (int x, int c, int s, int t, int p) { if (s == t) { d[p] = zuo[p] = you[p] = duo[p] = c; return; } int m = (s+t)>>1; if (x <= m) chenge (x, c, s, m, p<<1); if (x > m) chenge (x, c, m+1, t, (p<<1)|1); d[p] = (d[p<<1] + d[(p<<1)|1]); zuo[p] = max (zuo[p<<1], d[p<<1]+zuo[(p<<1)|1]); you[p] = max (you[(p<<1)|1], d[(p<<1)|1]+you[p<<1]); duo[p] = max (max(duo[p<<1], duo[(p<<1)|1]), you[p<<1]+zuo[(p<<1)|1]); }
inline int get_sum (int l, int r, int s, int t, int p) { if(l <= s && t <= r) return p; int m = (s + t)>>1, sum(0); if(r <= m) return get_sum (l, r, s, m, p<<1); if(l > m) return get_sum (l, r, m+1, t, (p<<1)|1); int t1 = get_sum (l, r, s, m, p<<1), t2 = get_sum (l, r, m+1,t, (p<<1)|1); int ans = ++ tot; d[ans] = d[t1] + d[t2]; zuo[ans] = max (zuo[t1], d[t1] + zuo[t2]); you[ans] = max (you[t2], d[t2] + you[t1]); duo[ans] = max (max (duo[t1], duo[t2]), you[t1] + zuo[t2]); return ans; }
main () { cin >> n >> m; tot=(n<<3)+1; for (int i = 1; i <= n; ++ i) cin >> a[i]; build (1, n, 1); for (int i = 1; i <= m; ++ i) { int opt, x, y; cin >> opt >> x >> y; if (opt == 1) { if (x > y) swap (x, y); cout << duo[get_sum(x, y, 1, n, 1)] << '\n'; } else chenge (x, y, 1, n, 1); } return 0; }
|