#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
struct FenwickTree {
vector<ll> bit;
FenwickTree(int n) {
bit.assign(n + 1, 0);
}
void add(int pos, ll val) {
for(++pos; pos < bit.size(); pos += pos & -pos)
bit[pos] += val;
}
ll sum(int pos) {
ll res = 0;
for(++pos; pos > 0; pos -= pos & -pos)
res += bit[pos];
return res;
}
ll rangeSum(int l, int r) {
return sum(r) - sum(l - 1);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> p(n + 1);
vector<int> pos(n + 1);
for(int i = 1; i <= n; i++) {
cin >> p[i];
pos[p[i]] = i;
}
FenwickTree normal(n);
FenwickTree permuted(n);
while(q--) {
int type;
cin >> type;
if(type == 0) {
int l, r, c;
cin >> l >> r >> c;
for(int i = l; i <= r; i++) {
normal.add(i - 1, c);
}
}
else if(type == 1) {
int l, r, c;
cin >> l >> r >> c;
for(int i = l; i <= r; i++) {
permuted.add(p[i] - 1, c);
}
}
else if(type == 2) {
int l, r;
cin >> l >> r;
cout << normal.rangeSum(l - 1, r - 1) << "\n";
}
else { // type == 3
int l, r;
cin >> l >> r;
ll sum = 0;
for(int i = l; i <= r; i++) {
int idx = p[i] - 1;
sum += normal.sum(idx) + permuted.sum(idx);
}
cout << sum << "\n";
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGwgPSBsb25nIGxvbmc7CgpzdHJ1Y3QgRmVud2lja1RyZWUgewogICAgdmVjdG9yPGxsPiBiaXQ7CiAgICAKICAgIEZlbndpY2tUcmVlKGludCBuKSB7CiAgICAgICAgYml0LmFzc2lnbihuICsgMSwgMCk7CiAgICB9CiAgICAKICAgIHZvaWQgYWRkKGludCBwb3MsIGxsIHZhbCkgewogICAgICAgIGZvcigrK3BvczsgcG9zIDwgYml0LnNpemUoKTsgcG9zICs9IHBvcyAmIC1wb3MpCiAgICAgICAgICAgIGJpdFtwb3NdICs9IHZhbDsKICAgIH0KICAgIAogICAgbGwgc3VtKGludCBwb3MpIHsKICAgICAgICBsbCByZXMgPSAwOwogICAgICAgIGZvcigrK3BvczsgcG9zID4gMDsgcG9zIC09IHBvcyAmIC1wb3MpCiAgICAgICAgICAgIHJlcyArPSBiaXRbcG9zXTsKICAgICAgICByZXR1cm4gcmVzOwogICAgfQogICAgCiAgICBsbCByYW5nZVN1bShpbnQgbCwgaW50IHIpIHsKICAgICAgICByZXR1cm4gc3VtKHIpIC0gc3VtKGwgLSAxKTsKICAgIH0KfTsKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgCiAgICBpbnQgbiwgcTsKICAgIGNpbiA+PiBuID4+IHE7CiAgICAKICAgIHZlY3RvcjxpbnQ+IHAobiArIDEpOwogICAgdmVjdG9yPGludD4gcG9zKG4gKyAxKTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgY2luID4+IHBbaV07CiAgICAgICAgcG9zW3BbaV1dID0gaTsKICAgIH0KICAgIAogICAgRmVud2lja1RyZWUgbm9ybWFsKG4pOwogICAgRmVud2lja1RyZWUgcGVybXV0ZWQobik7CiAgICAKICAgIHdoaWxlKHEtLSkgewogICAgICAgIGludCB0eXBlOwogICAgICAgIGNpbiA+PiB0eXBlOwogICAgICAgIAogICAgICAgIGlmKHR5cGUgPT0gMCkgewogICAgICAgICAgICBpbnQgbCwgciwgYzsKICAgICAgICAgICAgY2luID4+IGwgPj4gciA+PiBjOwogICAgICAgICAgICBmb3IoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgewogICAgICAgICAgICAgICAgbm9ybWFsLmFkZChpIC0gMSwgYyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZWxzZSBpZih0eXBlID09IDEpIHsKICAgICAgICAgICAgaW50IGwsIHIsIGM7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHIgPj4gYzsKICAgICAgICAgICAgZm9yKGludCBpID0gbDsgaSA8PSByOyBpKyspIHsKICAgICAgICAgICAgICAgIHBlcm11dGVkLmFkZChwW2ldIC0gMSwgYyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZWxzZSBpZih0eXBlID09IDIpIHsKICAgICAgICAgICAgaW50IGwsIHI7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHI7CiAgICAgICAgICAgIGNvdXQgPDwgbm9ybWFsLnJhbmdlU3VtKGwgLSAxLCByIC0gMSkgPDwgIlxuIjsKICAgICAgICB9CiAgICAgICAgZWxzZSB7ICAvLyB0eXBlID09IDMKICAgICAgICAgICAgaW50IGwsIHI7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHI7CiAgICAgICAgICAgIGxsIHN1bSA9IDA7CiAgICAgICAgICAgIGZvcihpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKSB7CiAgICAgICAgICAgICAgICBpbnQgaWR4ID0gcFtpXSAtIDE7CiAgICAgICAgICAgICAgICBzdW0gKz0gbm9ybWFsLnN1bShpZHgpICsgcGVybXV0ZWQuc3VtKGlkeCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dCA8PCBzdW0gPDwgIlxuIjsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9