https://www.luogu.com.cn/problem/P3374
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 1;
long long treeA[MAXN], treeB[MAXN];
int n;
int lowbit(int x) {
return x & -x;
}
void add(long long tree[], int pos, int val) {
while(pos <= n) {
tree[pos] += val;
pos += lowbit(pos);
}
}
long long query(long long tree[], int pos) {
long long res = 0;
while(pos > 0) {
res += tree[pos];
pos -= lowbit(pos);
}
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
int x;
cin >> x;
add(treeB, i, x);
}
for(int i = 0; i < m; ++i) {
int op;
cin >> op;
if(op == 1) {
int x, y, k;
cin >> x >> y >> k;
add(treeA, x, k);
add(treeA, y + 1, -k);
add(treeB, x, -k * (x - 1));
add(treeB, y + 1, k * y);
} else {
int x, y;
cin >> x >> y;
cout << query(treeA, y) * y + query(treeB, y) - query(treeA, x - 1) * (x - 1) - query(treeB, x - 1) << endl;
}
}
return 0;
}