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
| struct Segment { int l, r; ll dat, add; #define l(x) tree[x].l #define r(x) tree[x].r #define dat(x) tree[x].dat #define add(x) tree[x].add }tree[maxn << 2]; int a[maxn];
inline void update(int p) { dat(p) = dat(p<<1) + dat(p<<1|1); } void build(int p, int l, int r) { l(p) = l; r(p) = r; if (l == r) {dat(p) = a[l];return;} int mid = (l + r) >> 1; build(p<<1, l, mid); build(p<<1|1,mid+1, r); update(p); } void spread(int p) { if (add(p)) { dat(p<<1) += add(p) * (r(p<<1) - l(p<<1) + 1); dat(p<<1|1) += add(p) * (r(p<<1|1) - l(p<<1|1) + 1); add(p<<1) += add(p); add(p<<1|1) += add(p); add(p) = 0; } } void change(int p, int l, int r, int d) { if (l <= l(p) && r >= r(p)) { dat(p) += (long long)d * (r(p) - l(p) + 1); add(p) += d; return; } spread(p); int mid = (l(p) + r(p)) >> 1; if (l <= mid) change(p<<1, l, r, d); if (r > mid) change(p<<1|1, l, r, d); update(p); } long long ask(int p, int l, int r) { if (l <= l(p) && r >= r(p)) return dat(p); spread(p); int mid = (l(p) + r(p)) >> 1; long long res = 0; if (l <= mid) res += ask(p<<1, l, r); if (r > mid) res += ask(p<<1|1, l, r); return res; }
|