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
| template<class Info, class Tag> struct LazySegmentTree { int n; vector<Info> info; vector<Tag> tag;
LazySegmentTree() : n(0) {} LazySegmentTree(int n_, Info v_ = Info()) { init(n_, v_); } template<class T> LazySegmentTree(vector<T> init_) { init(init_); } void init(int n_, Info v_ = Info()) { init(vector<Info>(n_, v_)); } template<class T> void init(vector<T> init_) { n = init_.size(); info.assign(4 << __lg(n), Info()); tag.assign(4 << __lg(n), Tag()); 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 apply(int p, const Tag &v) { info[p].apply(v); tag[p].apply(v); }
void push(int p) { apply(2 * p, tag[p]); apply(2 * p + 1, tag[p]); tag[p] = Tag(); }
void modify(int l, int r, const Tag &v) { return modify(1, 0, n, l, r, v); } void modify(int p, int l, int r, int x, int y, const Tag &v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { apply(p, v); return; } int m = (l + r) / 2; push(p); modify(2 * p, l, m, x, y, v); modify(2 * p + 1, m, r, x, y, v); pull(p); }
Info query(int l, int r) { return query(1, 0, n, l, r); } Info query(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; push(p); return query(2 * p, l, m, x, y) + query(2 * p + 1, m, r, x, y); }
};
struct Tag { void apply(const Tag &t) & { } };
struct Info { void apply(const Tag &t) & { } };
Info operator+(const Info &a, const Info &b) { Info c; return c; }
|