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
| #include <iostream> #define ll long long #define fast std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); using namespace std; const int maxn = 100010; int a[maxn]; ll bit[2][maxn], sum[maxn]; int n, m;
void update(int k, int i, int x) { while (i <= n) { bit[k][i] += x; i += i & -i; } }
ll query(int k, int i) { long long ans = 0; while (i > 0) { ans += bit[k][i]; i -= i & -i; } return ans; }
int main() { fast cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } int opt, x, y, k; while (m--) { cin >> opt >> x >> y; if (opt == 1){ cin >> k; update(0,x,k); update(0,y+1,-k); update(1,x,x*k); update(1,y+1,-(y+1)*k); } else cout << (ll)sum[y] +(y+1)*query(0,y)-query(1,y)-(ll)(sum[x-1]+x*query(0,x-1)-query(1,x-1)) << "\n"; } }
|