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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <iostream> #include <cstdio> #include <cstring> #include <string>
typedef long long int ll; typedef unsigned long long int ull;
const int MAXN = 1000000 + 1;
ull n, m, a[MAXN], ans[MAXN << 2], tag[MAXN << 2];
inline ll ls(ll x) { return x << 1; }
inline ll rs(ll x) { return x << 1 | 1; }
void scan(){ scanf("%lld %lld", &n, &m); for (ll i = 1;i <= n;i++) { scanf("%lld", &a[i]); } }
inline void pushUp(ll p) { ans[p] = ans[ls(p)] + ans[rs(p)]; }
inline void build(ll p, ll l, ll r) { tag[p] = 0; if (l == r) { ans[p] = a[l]; return; } ll mid = (l + r) >> 1; build(ls(p), l, mid); build(rs(p), mid + 1, r); pushUp(p); }
inline void rec(ll p, ll l, ll r, ll k) { tag[p] = tag[p] + k; ans[p] = ans[p] + k * (r - l + 1); }
inline void pushDown(ll p,ll l, ll r) { ll mid = (l + r) >> 1; rec(ls(p), l, mid, tag[p]); rec(rs(p), mid + 1, r, tag[p]); tag[p] = 0; }
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) { if (nl <= l && r <= nr) { ans[p] += k * (r - l + 1); tag[p] += k; return; } pushDown(p, l, r); ll mid = (l + r) >> 1; if (nl <= mid) update(nl, nr, l, mid, ls(p), k); if (nr > mid) update(nl, nr, mid + 1, r, rs(p), k); pushUp(p); }
ll query(ll qx, ll qy, ll l, ll r, ll p) { ll res = 0; if (qx <= l && r <= qy) return ans[p]; ll mid = (l + r) >> 1; pushDown(p, l, r); if (qx <= mid) res += query(qx, qy, l, mid, ls(p)); if (qy > mid) res += query(qx, qy, mid + 1, r, rs(p)); return res; }
int main() { ll a1, b, c, d, e, f; scan(); build(1, 1, n); while (m--) { scanf("%lld", &a1); switch(a1) { case 1:{ scanf("%lld %lld %lld", &b, &c, &d); update(b, c, 1, n, 1, d); break; } case 2:{ scanf("%lld %lld", &e, &f); printf("%lld\n", query(e, f, 1, n, 1)); break; } } } return 0; }
|