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
| template<class Info> struct SegmentTree { int n; std::vector<Info> info; SegmentTree() : n(0) {} SegmentTree(int n_, Info v_ = Info()) { init(n_, v_); } template<class T> SegmentTree(std::vector<T> init_) { init(init_); } void init(int n_, Info v_ = Info()) { init(std::vector(n_, v_)); } template<class T> void init(std::vector<T> init_) { n = init_.size(); info.assign(4 << std::__lg(n), Info()); std::function<void(int, int, int)> build = [&](int p, int l, int r) { if (r - l == 1) { info[p] = init_[l]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); pull(p); }; build(1, 0, n); } void pull(int p) { info[p] = info[2 * p] + info[2 * p + 1]; } void modify(int p, int l, int r, int x, const Info &v) { if (r - l == 1) { info[p] = v; return; } int m = (l + r) / 2; if (x < m) { modify(2 * p, l, m, x, v); } else { modify(2 * p + 1, m, r, x, v); } pull(p); } void modify(int p, const Info &v) { modify(1, 0, n, p, v); } Info rangeQuery(int p, int l, int r, int x, int y) { if (l >= y || r <= x) { return Info(); } if (l >= x && r <= y) { return info[p]; } int m = (l + r) / 2; return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y); } Info rangeQuery(int l, int r) { return rangeQuery(1, 0, n, l, r); } };
constexpr int inf = 1E9 + 1; struct Info { int cnt; int minPre; int maxPre; int minSuf; int maxSuf; int mx = -inf; }; Info get(int x) { return { 1, x + 1, x - 1, x + 1, x - 1, 0 }; } Info operator+(const Info &a, const Info &b) { return { a.cnt + b.cnt, min(a.minPre, a.cnt + b.minPre), max(a.maxPre, b.maxPre - a.cnt), min(b.minSuf, b.cnt + a.minSuf), max(b.maxSuf, a.maxSuf - b.cnt), max({a.mx, b.mx, -a.minSuf + b.maxPre + 1, a.maxSuf - b.minPre + 1}) }; }
void solve() { int n = read(); int m = read(); vector<int> a(n); for (auto &x : a) x = read();
SegmentTree<Info> seg(n); for (int i = 0; i < n; i++) { seg.modify(i, get(a[i])); } cout << seg.rangeQuery(0, n).mx << '\n'; for (int i = 1; i <= m; i++) { int p = read() - 1, x = read(); seg.modify(p, get(x)); cout << seg.rangeQuery(0, n).mx << '\n'; } }
|