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
| struct SegmentTree{ int n; vector<Info> info;
SegmentTree() {}
SegmentTree(int n, Info _init = Info()){ init(vector<Info>(n, _init)); }
SegmentTree(const vector<Info> &_init){ init(_init); }
void init(const vector<Info> &_init){ n = (int)_init.size(); info.assign((n << 2) + 1, Info()); function<void(int, int, int)> build = [&](int p, int l, int r){ if (l == r){ info[p] = _init[l - 1]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m + 1, r); pull(p); }; build(1, 1, 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 (l == r){ 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 + 1, r, x, v); } pull(p); }
void modify(int p, const Info &v){ modify(1, 1, n, p, v); }
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; return query(2 * p, l, m, x, y) + query(2 * p + 1, m + 1, r, x, y); }
Info query(int l, int r){ return query(1, 1, n, l, r); } };
|