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
| struct Node { Node *lc, *rc; int l, r; long long val; int tag; inline void clear() { val = tag = 0; } inline void cover(const int delta) { tag += delta, val += (long long)delta * (r - l + 1); } inline void pushDown() { if (tag) lc->cover(tag), rc->cover(tag), tag = 0; } inline void update(const int l, const int r, const int delta) { if (l > this->r || r < this->l) return; else if (l <= this->l && r >= this->r) cover(delta); else pushDown(), lc->update(l, r, delta), rc->update(l, r, delta), val = lc->val + rc->val; } inline long long query(const int l, const int r) { register long long ret = 0; if (l > this->r || r < this->l) return 0; else if (l <= this->l && r >= this->r) return val; else return pushDown(), ret += lc->query(l, r), ret += rc->query(l, r), ret; } } pool[(MAXN + 10) << 4], *root, *tail = pool; inline void build(Node *&rt, const int l, const int r, int *a) { rt = tail++, rt->clear(), rt->l = l, rt->r = r; if (l == r) { rt->val = a[l]; return; } register int mid = l + r >> 1; build(rt->lc, l, mid, a), build(rt->rc, mid + 1, r, a); rt->val = rt->lc->val + rt->rc->val; }
|