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
| #include <iostream> typedef long long ll; const int N = 100000;
struct Node { ll sum, tag; int l, r; Node *ls, *rs; inline int len(); void maintain(); void pushdown(); ll query(); void add(); Node *build(int, int); void print(); }; Node T[N * 4]; Node *null; int cnt = 1, L, R, X;
int Node::len() { return r - l + 1; } void Node::maintain() { sum = ls->sum + rs->sum; } void Node::pushdown() { ls->tag += tag, rs->tag += tag; ls->sum += ls->len() * tag, rs->sum += rs->len() * tag; tag = 0; null->sum = null->tag = 0; } ll Node::query() { if (L <= l && r <= R) return sum; if (r < L || R < l) return 0; pushdown(); return ls->query() + rs->query(); } void Node::add() { if (r < L || R < l) return; if (L <= l && r <= R) tag += X, sum += len() * X; else pushdown(), ls->add(), rs->add(), maintain(); } Node *Node::build(int l, int r) { Node *ret = &T[cnt++]; ret->l = l, ret->r = r, ret->ls = ret->rs = null; if (l == r) cin >> ret->sum; else { int mid = (l + r) / 2; ret->ls = build(l, mid), ret->rs = build(mid + 1, r); ret->maintain(); } return ret; }
void Node::print() { if (this == null) return; cerr << l << ' ' << r << ' ' << sum << ' ' << tag << endl; ls->print(), rs->print(); }
int main() { std::ios::sync_with_stdio(false); cout.tie(0); int n, m; null = T; null->ls = null->rs = null; cin >> n >> m; Node *rt = null->build(1, n); L = 1, R = n; while (m--) { int opt; cin >> opt >> L >> R; if (opt == 1) { cin >> X; rt->add(); } else cout << rt->query() << '\n'; } return 0; }
|