#include<iostream>
using namespace std;
#define lc p<<1
#define rc p<<1|1
#define ll long long
#define N 100005
int n;
int w[N];
struct tr {
int l;
int r;
int sum;
int add;
}tr[N*4];
void pushdown(int p) {
if (tr[p].add)
{
tr[lc].sum += (tr[lc].r - tr[lc].l + 1) * tr[p].add;
tr[lc].add += tr[p].add;
tr[rc].sum += (tr[rc].r - tr[rc].l + 1) * tr[p].add;
tr[rc].add += tr[p].add;
tr[p].add = 0;
}
}
void pushup(int p) {
tr[p].sum = tr[lc].sum + tr[rc].sum;
}
void build(int p, int l, int r) {
tr[p] = { l,r,w[l],0 };
if (l == r)return;
int m = (l + r) >> 1;
build(lc, l, m);
build(rc, m + 1, r);
pushup(p);
}
void update(int p, int x, int y, int k)
{
if (x <= tr[p].l && tr[p].r <= y) {
tr[p].sum += (tr[p].r - tr[p].l+1) * k;
tr[p].add += k;
return;
}
int m = tr[p].l + tr[p].r >> 1;
pushdown(p);
if (x <= m)update(lc, x, y, k);
if (y > m)update(rc, x, y, k);
pushup(p);
}
ll query(int p, int x, int y) {
if (x <= tr[p].l && tr[p].r <= y)
{
return tr[p].sum;
}
int m = (tr[p].l + tr[p].r) >> 1;
pushdown(p);
ll sum = 0;
if (x <= m) sum += query(lc, x, y);
if (y > m) sum += query(rc, x, y);
return sum;
}
int main() {
cin >> n;
int m;
cin >> m;
int s;
int c;
int x, y, k;
for (int i = 1; i <= n; i++)
{
cin >> s;
w[i] = s;
}
build(1, 1, n);
for (int i = 1; i <= m; i++)
{
cin >> c;
if (c == 1)
{
cin >> x >> y >> k;
update(1, x, y,k);
}
else if( c==2 ) {
cin >> x >> y;
ll add = query(1, x, y);
cout << add << endl;
}
}
}
在最后三组数据wa了,是加法溢出了吗?