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
| #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <algorithm> #define MAXN (100000 + 5) #define LL long long #define LS(x) ((x) << 1) #define RS(x) (((x) << 1) | 1) using namespace std; struct node { int le, ri, lazy; LL zh; } tree[MAXN << 3]; int a[MAXN]; void build(int now, int le, int ri) { tree[now].le = le, tree[now].ri = ri; if (le == ri) { tree[now].zh = a[le]; return ; } int mi = (le + ri) >> 1; build(LS(now), le, mi), build(RS(now), mi + 1, ri); tree[now].zh = tree[LS(now)].zh + tree[RS(now)].zh; return ; } void push_down(int now) { int& zh = tree[now].lazy; tree[LS(now)].lazy += zh, tree[RS(now)].lazy += zh; tree[LS(now)].zh += (LL)(tree[LS(now)].ri - tree[LS(now)].le + 1) * zh; tree[RS(now)].zh += (LL)(tree[RS(now)].ri - tree[RS(now)].le + 1) * zh; zh = 0; } void adjust(int now, int tle, int tri, int k) { tree[now].zh += (LL)(tri - tle + 1) * k; push_down(now); if (tree[now].le == tle && tree[now].ri == tri) { tree[now].lazy += k; return ; } int mi = (tree[now].le + tree[now].ri) >> 1; if (tri <= mi) adjust(LS(now), tle, tri, k); else if (tle > mi) adjust(RS(now), tle, tri, k); else adjust(LS(now), tle, mi, k), adjust(RS(now), mi + 1, tri, k); return ; } LL enquiry(int now, int le, int ri) { if (tree[now].le == le && tree[now].ri == ri) return tree[now].zh; push_down(now); int mi = (tree[now].le + tree[now].ri) >> 1; if (ri <= mi) return enquiry(LS(now), le, ri); else if (le > mi) return enquiry(RS(now), le, ri); else return enquiry(LS(now), le, mi) + enquiry(RS(now), mi + 1, ri); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); build(1, 1, n); for (int i = 1, a, b, c, d; i <= m; i++) { scanf("%d%d%d", &a, &b, &c); if (a == 1) { scanf("%d", &d); adjust(1, b, c, d); } else printf("%lld\n", enquiry(1, b, c)); } return 0; }
|