#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
class FenwickTree {
private:
vector<ll> bit;
int n;
public:
FenwickTree(int size) : n(size) {
bit.resize(n + 1, 0);
}
void add(int idx, ll val) {
idx++;
while (idx <= n) {
bit[idx] += val;
idx += idx & (-idx);
}
}
ll sum(int idx) {
idx++;
ll res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & (-idx);
}
return res;
}
ll rangeSum(int l, int r) {
return sum(r) - (l > 0 ? sum(l - 1) : 0);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> p(n);
vector<int> pos(n + 1); // Position de chaque élément dans p
for (int i = 0; i < n; i++) {
cin >> p[i];
pos[p[i]] = i;
}
FenwickTree ft1(n); // Pour les mises à jour de type 0
FenwickTree ft2(n); // Pour les mises à jour de type 1
while (q--) {
int type;
cin >> type;
if (type == 0) {
int l, r, x;
cin >> l >> r >> x;
l--; r--;
ft1.add(l, x);
ft1.add(r + 1, -x);
}
else if (type == 1) {
int l, r, x;
cin >> l >> r >> x;
l--; r--;
ft2.add(l, x);
ft2.add(r + 1, -x);
}
else if (type == 2) {
int l, r;
cin >> l >> r;
l--; r--;
cout << ft1.rangeSum(l, r) << "\n";
}
else { // type == 3
int l, r;
cin >> l >> r;
l--; r--;
ll result = 0;
for (int i = l; i <= r; i++) {
int actualPos = pos[i + 1];
result += ft1.rangeSum(actualPos, actualPos) + ft2.rangeSum(i, i);
}
cout << result << "\n";
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGwgPSBsb25nIGxvbmc7CgpjbGFzcyBGZW53aWNrVHJlZSB7CnByaXZhdGU6CiAgICB2ZWN0b3I8bGw+IGJpdDsKICAgIGludCBuOwoKcHVibGljOgogICAgRmVud2lja1RyZWUoaW50IHNpemUpIDogbihzaXplKSB7CiAgICAgICAgYml0LnJlc2l6ZShuICsgMSwgMCk7CiAgICB9CgogICAgdm9pZCBhZGQoaW50IGlkeCwgbGwgdmFsKSB7CiAgICAgICAgaWR4Kys7CiAgICAgICAgd2hpbGUgKGlkeCA8PSBuKSB7CiAgICAgICAgICAgIGJpdFtpZHhdICs9IHZhbDsKICAgICAgICAgICAgaWR4ICs9IGlkeCAmICgtaWR4KTsKICAgICAgICB9CiAgICB9CgogICAgbGwgc3VtKGludCBpZHgpIHsKICAgICAgICBpZHgrKzsKICAgICAgICBsbCByZXMgPSAwOwogICAgICAgIHdoaWxlIChpZHggPiAwKSB7CiAgICAgICAgICAgIHJlcyArPSBiaXRbaWR4XTsKICAgICAgICAgICAgaWR4IC09IGlkeCAmICgtaWR4KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgIH0KCiAgICBsbCByYW5nZVN1bShpbnQgbCwgaW50IHIpIHsKICAgICAgICByZXR1cm4gc3VtKHIpIC0gKGwgPiAwID8gc3VtKGwgLSAxKSA6IDApOwogICAgfQp9OwoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CgogICAgaW50IG4sIHE7CiAgICBjaW4gPj4gbiA+PiBxOwoKICAgIHZlY3RvcjxpbnQ+IHAobik7CiAgICB2ZWN0b3I8aW50PiBwb3MobiArIDEpOyAgLy8gUG9zaXRpb24gZGUgY2hhcXVlIMOpbMOpbWVudCBkYW5zIHAKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgY2luID4+IHBbaV07CiAgICAgICAgcG9zW3BbaV1dID0gaTsKICAgIH0KCiAgICBGZW53aWNrVHJlZSBmdDEobik7ICAvLyBQb3VyIGxlcyBtaXNlcyDDoCBqb3VyIGRlIHR5cGUgMAogICAgRmVud2lja1RyZWUgZnQyKG4pOyAgLy8gUG91ciBsZXMgbWlzZXMgw6Agam91ciBkZSB0eXBlIDEKCiAgICB3aGlsZSAocS0tKSB7CiAgICAgICAgaW50IHR5cGU7CiAgICAgICAgY2luID4+IHR5cGU7CgogICAgICAgIGlmICh0eXBlID09IDApIHsKICAgICAgICAgICAgaW50IGwsIHIsIHg7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHIgPj4geDsKICAgICAgICAgICAgbC0tOyByLS07CiAgICAgICAgICAgIGZ0MS5hZGQobCwgeCk7CiAgICAgICAgICAgIGZ0MS5hZGQociArIDEsIC14KTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAodHlwZSA9PSAxKSB7CiAgICAgICAgICAgIGludCBsLCByLCB4OwogICAgICAgICAgICBjaW4gPj4gbCA+PiByID4+IHg7CiAgICAgICAgICAgIGwtLTsgci0tOwogICAgICAgICAgICBmdDIuYWRkKGwsIHgpOwogICAgICAgICAgICBmdDIuYWRkKHIgKyAxLCAteCk7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKHR5cGUgPT0gMikgewogICAgICAgICAgICBpbnQgbCwgcjsKICAgICAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICAgICAgbC0tOyByLS07CiAgICAgICAgICAgIGNvdXQgPDwgZnQxLnJhbmdlU3VtKGwsIHIpIDw8ICJcbiI7CiAgICAgICAgfQogICAgICAgIGVsc2UgeyAgLy8gdHlwZSA9PSAzCiAgICAgICAgICAgIGludCBsLCByOwogICAgICAgICAgICBjaW4gPj4gbCA+PiByOwogICAgICAgICAgICBsLS07IHItLTsKICAgICAgICAgICAgbGwgcmVzdWx0ID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKSB7CiAgICAgICAgICAgICAgICBpbnQgYWN0dWFsUG9zID0gcG9zW2kgKyAxXTsKICAgICAgICAgICAgICAgIHJlc3VsdCArPSBmdDEucmFuZ2VTdW0oYWN0dWFsUG9zLCBhY3R1YWxQb3MpICsgZnQyLnJhbmdlU3VtKGksIGkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvdXQgPDwgcmVzdWx0IDw8ICJcbiI7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAwOwp9