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
| #include <cstdio>
using namespace std; using LL = long long; const int N = 1000001; int n, q, x, y, z; LL s1[N], s2[N];
int lowbit(int x) { return x & -x; }
void add(int i, LL k) { for (int x = i; i <= n; i += lowbit(i)) s1[i] += k, s2[i] += k * x; }
LL getSum(int i) { LL s = 0; for (int x = i + 1; i > 0; i -= lowbit(i)) s += s1[i] * x - s2[i]; return s; }
int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%d", &y); LL k = y - x; s1[i] += k, s2[i] += k * i; int j = i + lowbit(i); if (j <= n) s1[j] += s1[i], s2[j] += s2[i]; x = y; } for (int i = 0; i < q; ++i) { scanf("%d", &x); if (x == 1) { scanf("%d%d%d", &x, &y, &z); add(x, z); add(y + 1, -z); } else { scanf("%d%d", &x, &y); printf("%lld\n", getSum(y) - getSum(x - 1)); } } return 0; }
|