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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, l, r) for (int i = (l); i >= (r); --i) using std::cerr; using std::cin; using std::cout; using std::endl; using std::make_pair; using std::pair; typedef pair<int, int> pii; typedef long long ll; typedef unsigned int ui;
const int inf = 500000000;
struct Node { int l, r; int el, er, m, s; Node *ls, *rs; } T[2000000]; int cnt = 0;
void upd(Node* c) { c->m = std::max(c->ls->m, std::max(c->rs->m, c->ls->er + c->rs->el)); c->el = std::max(c->ls->el, c->ls->s + c->rs->el); c->er = std::max(c->rs->er, c->ls->er + c->rs->s); c->s = c->ls->s + c->rs->s; }
void build(Node*& c, int l, int r) { c = &T[cnt++]; c->l = l, c->r = r; if (l == r) { cin >> c->m; c->s = c->el = c->er = c->m; return; } int mid = (l + r) / 2; build(c->ls, l, mid), build(c->rs, mid + 1, r); upd(c); }
int P, S; void e(Node* c) { if (c->l == c->r) { c->s = c->el = c->er = c->m = S; return; } if (P <= c->ls->r) e(c->ls); else e(c->rs); upd(c); }
int ans = 0, L, R; pii q(Node* c) { if (L <= c->l && c->r <= R) { ans = std::max(ans, c->m); return pii(c->el, c->er); } int mid = c->ls->r; if (R <= mid) return q(c->ls); else if (mid < L) return q(c->rs); else { pii x = q(c->ls), y = q(c->rs); ans = std::max(ans, x.second + y.first); return pii(std::max(x.first, c->ls->s + y.first), std::max(y.second, c->rs->s + x.second)); } }
int main() { #ifdef LOCAL freopen("input", "r", stdin); #endif std::ios::sync_with_stdio(false); cout.tie(0); int n, m; cin >> n >> m; Node* rt = nullptr; build(rt, 1, n); while (m--) { int k; cin >> k; if (k == 1) { cin >> L >> R; if (L > R) std::swap(L, R); ans = -inf; q(rt); cout << ans << endl; } else { cin >> P >> S; e(rt); } } return 0; }
|