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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 100000 * 10 + 10;
ll tag[N], sum[N]; int n, m;
#define lc (id << 1) #define rc (id << 1 | 1) void build(int id, int l, int r) { int mid = (l + r) >> 1; if(l == r) { scanf("%lld", &sum[id]); } else { build(lc, l, mid), build(rc, mid + 1, r); sum[id] = sum[lc] + sum[rc]; } }
void push(int id, int l, int r) { if(tag[id]) { sum[id] += tag[id] * (r - l + 1); tag[lc] += tag[id], tag[rc] += tag[id]; tag[id] = 0; } }
void up(int id, int l, int r) { int mid = (l + r) >> 1; push(lc, l, mid), push(rc, mid + 1, r); sum[id] = sum[lc] + sum[rc]; }
void modify(int id, int l, int r, int ql, int qr, ll x) { int mid = (l + r) >> 1; push(id, l, r); if(ql <= l && r <= qr) { tag[id] += x; return ; } else if(qr <= mid) { modify(lc, l, mid, ql, qr, x); } else if(ql >= mid + 1) { modify(rc, mid + 1, r, ql, qr, x); } else { modify(lc, l, mid, ql, mid, x); modify(rc, mid + 1, r, mid + 1, qr, x); } up(id, l, r); }
ll ask(int id, int l, int r, int ql, int qr) { int mid = (l + r) >> 1; push(id, l, r); if(ql <= l && r <= qr) { return sum[id]; } else if(qr <= mid) { return ask(lc, l, mid, ql, qr); } else if(ql >= mid + 1) { return ask(rc, mid + 1, r, ql, qr); } else { return ask(lc, l, mid, ql, mid) + ask(rc, mid + 1, r, mid + 1, qr); } }
int main() { scanf("%d%d", &n, &m); build(1, 1, n); for(int i = 1, op, l, r ; i <= m ; ++ i) { scanf("%d%d%d", &op, &l, &r); if(op == 1) { ll k; scanf("%lld", &k); modify(1, 1, n, l, r, k); } else { printf("%lld\n", ask(1, 1, n, l, r)); } } }
|