/*
https://www.acwing.com/solution/content/44886/
看acwing
*/
#include <bits/stdc++.h>
#define CLOSE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
typedef long long LL;
const int N = 1e6 + 10, M = N, mod = 1e9 + 7;
using namespace std;
int n, q;
LL t1[N], t2[N], a[N], b[N];
int lowbit(int x) {return x & -x;}
void add(int x, LL c, LL t[]){
for(int i = x; i <= n; i += lowbit(i)){
t[i] += c;
}
}
LL getsum(int x, LL t[]){
LL sum = 0;
for(int i = x; i; i -= lowbit(i)){
sum += t[i];
}
return sum;
}
LL query(int x){
return getsum(x, t1) * (x + 1) - getsum(x, t2);
}
int main()
{
CLOSE;
cin >> n >> q;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
for(int i = 1; i <= n; i ++){
b[i] = a[i] - a[i - 1];
add(i, b[i], t1);
add(i, b[i] * i, t2);
}
while(q --){
int op;
cin >> op;
if(op == 1){
int l, r;
LL k;
cin >> l >> r >> k;
add(r + 1, -k, t1), add(l, k, t1);//改差分数组
add(r + 1, -k * (r + 1), t2), add(l, k * l, t2);//改i*差分数组
} else{
int l, r;
cin >> l >> r;
cout << query(r) - query(l - 1) << endl;
}
}
return 0;
}
标签:树状,int,LL,t2,cin,t1,add,数组,区间
From: https://www.cnblogs.com/acwhr/p/18001294