https://chat.deepseek.com/share/zbhnw7xp6grkwv7hz7
#include <bits/stdc++.h>
using namespace std;
#define rp p << 1 | 1
#define lp p << 1
typedef long long ll;
const int N = 1e5 + 10;
ll n, m, a[N];
struct node {
ll l, r, sum, lazy;
}tr[4 * N];
void pushup(int p) {
tr[p].sum = tr[lp].sum + tr[rp].sum;
}
void pushdown(int p) {
if (tr[p].lazy) {
tr[lp].sum += tr[p].lazy * (tr[lp].r - tr[lp].l + 1);
tr[lp].lazy += tr[p].lazy;
tr[rp].sum += tr[p].lazy * (tr[rp].r - tr[rp].l + 1);
tr[rp].lazy += tr[p].lazy;
tr[p].lazy = 0;
}
}
void build(int p, int l, int r) {
tr[p] = (node){l, r, a[l], 0};
if (l == r) return ;
int mid = l + (r - l) / 2;
build(lp, l, mid);
build(rp, mid + 1, r);
pushup(p);
}
void change(int p, int x, int y, ll k) {
if (x > tr[p].r || y < tr[p].l) return ;
if (x <= tr[p].l && y >= tr[p].r) {
tr[p].sum += k * (tr[p].r - tr[p].l + 1);
tr[p].lazy += k;
return ;
}
pushdown(p);
int mid = tr[p].l + (tr[p].r - tr[p].l) / 2;
if (x <= mid) change(lp, x, y, k);
if (y > mid) change(rp, x, y, k);
pushup(p);
}
ll ask(int p, int x, int y) {
ll res=0;
if (x > tr[p].r || y < tr[p].l) return 0;
if (x <= tr[p].l && y >= tr[p].r) return tr[p].sum;
pushdown(p);
int mid = tr[p].l + (tr[p].r - tr[p].l) / 2;
if (x <= mid) res += ask(lp, x, y);
if (y > mid) res += ask(rp, x, y);
return res;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
build(1, 1, n);
for (int i = 1; i <= m; ++i) {
ll op, x, y, k;
cin >> op >> x >> y;
if (op == 1) {
cin >> k;
change(1, x, y, k);
} else cout << ask(1, x, y) << "\n";
}
return 0;
}/*
# # # #
# # # #
# # # # # # # # #
# # # # # # # # # # #
# # # # # # # # #
# # # #
# # # #
/\ /\
/ \/ \ __________
/ /\ \/ \
/ / \ / ___ \
/ / \/ / \ |
/ / \ |
/ |_____| /
/ /____________
/________________/ \_________ /\ /\
\ \ / \__\___________
\ / //\ \
\ / \// \ \
/ \______ / | \ \
/ / \___________/____| \ \
/ / | | \ \ /
/\ / / |\ | | \ \ /
/ \/ / | \| | \_____\________/
/ / | | \ \
/ / | | \ \
/ / | | \ \
/ / | | \ \
*/