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
| #include <bits/stdc++.h> using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int n; ll t1[N],t2[N],a[N]; int lowbit(int x) { return x & -x; } void init() { for(int i = 1;i <= n;i++) { ll k = a[i] - a[i - 1]; t1[i] += k;t2[i] += k * (i - 1); int j = i + lowbit(i); if(j <= n) t1[j] += t1[i], t2[j] += t2[i]; } } void add(int x,ll k) { ll i = x - 1; while(x <= n) { t1[x] += k; t2[x] += k * i; x += lowbit(x); } } ll getsum(int x) { ll res = 0,i = x; while(x >= 1) { res += i * t1[x] - t2[x]; x -= lowbit(x); } return res; } int main() { int T; scanf("%d%d",&n,&T); for(int i = 1;i <= n;i++) scanf("%lld",a + i); init(); while(T--) { int op,l,r; scanf("%d%d%d",&op,&l,&r); if(op == 1) { ll k; scanf("%lld",&k); add(l,k);add(r + 1,-k); } else { printf("%lld\n",getsum(r) - getsum(l - 1)); } } return 0; }
|