区间修改,询问,带有 lazy tag。
# include <stdio.h> # define ls ((r) << 1) # define rs ((r) << 1 | 1) # define mid (((x)+(y))>>1) # define MAXN 100005 typedef long long int LL; int a[MAXN]; LL sum[MAXN * 4]; LL lazy[MAXN * 4]; void update(int r) { sum[r] = sum[ls] + sum[rs]; } void build(int x, int y, int r) { sum[r] = 0; lazy[r] = 0; if (x == y) { sum[r] = a[x]; return ; } build(x, mid, ls); build(mid+1, y, rs); update(r); } void pushdown(int x, int y, int r) { if (lazy[r]) { lazy[ls] += lazy[r], lazy[rs] += lazy[r]; sum[ls] += lazy[r]*(mid-x+1); sum[rs] += lazy[r]*(y-mid); lazy[r] = 0; } } void add(int x, int y, int r, int s, int t, LL val) { if (s<=x && y<=t) { lazy[r] += val; sum[r] += val*(y-x+1); return ; } pushdown(x, y, r); if (s<=mid) add(x, mid, ls, s, t, val); if (mid+1<=t) add(mid+1, y, rs, s, t, val); update(r); } void query(int x, int y, int r, int s, int t, LL *p) { if (s<=x && y<=t) { *p += sum[r]; return ; } pushdown(x, y, r); if (s<=mid) query(x, mid, ls, s, t, p); if (mid+1<=t) query(mid+1, y, rs, s, t, p); } int main() { LL ans; char od[5]; int i, n, m, s, t, val; while (~scanf("%d%d", &n, &m)) { for (i = 1; i <= n; ++i) scanf("%d", &a[i]); build(1, n, 1); for (i = 1; i <= m; ++i) { scanf("%s%d%d", od, &s, &t); if (od[0] == 'C') scanf("%d", &val), add(1, n, 1, s, t, val); else ans = 0, query(1, n, 1, s, t, &ans), printf("%lld\n", ans); } } return 0; }