题意:
区间增加一个数。(-)
求区间的和。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> #include <stack> #include <vector> #include <queue> #include <map> #include <climits> #include <cassert> #define LL long long #define lson lo, mi, rt << 1 #define rson mi + 1, hi, rt << 1 | 1 using namespace std; const int maxn = 100000 + 10; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const double pi = acos(-1.0); const double ee = exp(1.0); LL add[maxn << 2]; LL sum[maxn << 2]; void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void pushdown(int rt, int mi) { if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; sum[rt << 1] += add[rt] * (mi - (mi >> 1)); sum[rt << 1 | 1] += add[rt] * (mi >> 1); add[rt] = 0; } } void build(int lo, int hi, int rt) { add[rt] = 0; if (lo == hi) { scanf("%lld", &sum[rt]); return; } int mi = (lo + hi) >> 1; build(lson); build(rson); pushup(rt); } void update(int L, int R, int c, int lo, int hi, int rt) { if (L <= lo && hi <= R) { add[rt] += (LL)c; sum[rt] += (LL)c * (hi - lo + 1); return; } pushdown(rt, hi - lo + 1); int mi = (lo + hi) >> 1; if (L <= mi) update(L, R, c, lson); if (mi < R) update(L, R, c, rson); pushup(rt); } LL query(int L, int R, int lo, int hi, int rt) { if (L <= lo && hi <= R) { return sum[rt]; } pushdown(rt, hi - lo + 1); int mi = (lo + hi) >> 1; LL res = 0; if (L <= mi) res += query(L, R, lson); if (mi < R) res += query(L, R, rson); return res; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int n, q; while (~scanf("%d%d", &n, &q)) { build(1, n, 1); while (q--) { char op[2]; int fr, to, num; scanf("%s", op); if (op[0] == 'Q') { scanf("%d%d", &fr, &to); printf("%lld\n", query(fr, to, 1, n, 1)); } else { scanf("%d%d%d", &fr, &to, &num); update(fr, to, num, 1, n, 1); } } } return 0; }