#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 5;
int A[N], P[N], P_map[N];
vector<int> segTree(4 * N);
void build(int node, int start, int end) {
if (start == end) {
segTree[node] = A[start];
} else {
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
segTree[node] = segTree[2 * node] + segTree[2 * node + 1];
}
}
void update(int node, int start, int end, int idx, int val) {
if (start == end) {
segTree[node] += val;
} else {
int mid = (start + end) / 2;
if (idx <= mid) {
update(2 * node, start, mid, idx, val);
} else {
update(2 * node + 1, mid + 1, end, idx, val);
}
segTree[node] = segTree[2 * node] + segTree[2 * node + 1];
}
}
int query(int node, int start, int end, int l, int r) {
if (r < start || end < l) {
return 0;
}
if (l <= start && end <= r) {
return segTree[node];
}
int mid = (start + end) / 2;
int leftSum = query(2 * node, start, mid, l, r);
int rightSum = query(2 * node + 1, mid + 1, end, l, r);
return leftSum + rightSum;
}
int main() {
int N, Q;
cin >> N >> Q;
// Initialize A to 0
for (int i = 1; i <= N; i++) {
A[i] = 0;
}
// Read the permutation P
for (int i = 1; i <= N; i++) {
cin >> P[i];
P_map[P[i]] = i;
}
// Build the Segment Tree
build(1, 1, N);
while (Q--) {
int type;
cin >> type;
if (type == 0 || type == 1) {
int l, r, c;
cin >> l >> r >> c;
if (type == 0) {
// Update the range [l, r] in A
for (int i = l; i <= r; i++) {
update(1, 1, N, i, c);
}
} else {
// Update the range [P[l], P[r]] in A
for (int i = l; i <= r; i++) {
update(1, 1, N, P_map[i], c);
}
}
} else if (type == 2 || type == 3) {
int l, r;
cin >> l >> r;
if (type == 2) {
// Query the sum of the range [l, r] in A
cout << query(1, 1, N, l, r) << endl;
} else {
// Query the sum of the range [P[l], P[r]] in A
int sum = 0;
for (int i = l; i <= r; i++) {
sum += query(1, 1, N, P_map[i], P_map[i]);
}
cout << sum << endl;
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcg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RhcnQsIGludCBlbmQsIGludCBsLCBpbnQgcikgewogICAgaWYgKHIgPCBzdGFydCB8fCBlbmQgPCBsKSB7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CiAgICBpZiAobCA8PSBzdGFydCAmJiBlbmQgPD0gcikgewogICAgICAgIHJldHVybiBzZWdUcmVlW25vZGVdOwogICAgfQogICAgaW50IG1pZCA9IChzdGFydCArIGVuZCkgLyAyOwogICAgaW50IGxlZnRTdW0gPSBxdWVyeSgyICogbm9kZSwgc3RhcnQsIG1pZCwgbCwgcik7CiAgICBpbnQgcmlnaHRTdW0gPSBxdWVyeSgyICogbm9kZSArIDEsIG1pZCArIDEsIGVuZCwgbCwgcik7CiAgICByZXR1cm4gbGVmdFN1bSArIHJpZ2h0U3VtOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBOLCBROwogICAgY2luID4+IE4gPj4gUTsKCiAgICAvLyBJbml0aWFsaXplIEEgdG8gMAogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gTjsgaSsrKSB7CiAgICAgICAgQVtpXSA9IDA7CiAgICB9CgogICAgLy8gUmVhZCB0aGUgcGVybXV0YXRpb24gUAogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gTjsgaSsrKSB7CiAgICAgICAgY2luID4+IFBbaV07CiAgICAgICAgUF9tYXBbUFtpXV0gPSBpOwogICAgfQoKICAgIC8vIEJ1aWxkIHRoZSBTZWdtZW50IFRyZWUKICAgIGJ1aWxkKDEsIDEsIE4pOwoKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBpbnQgdHlwZTsKICAgICAgICBjaW4gPj4gdHlwZTsKCiAgICAgICAgaWYgKHR5cGUgPT0gMCB8fCB0eXBlID09IDEpIHsKICAgICAgICAgICAgaW50IGwsIHIsIGM7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHIgPj4gYzsKCiAgICAgICAgICAgIGlmICh0eXBlID09IDApIHsKICAgICAgICAgICAgICAgIC8vIFVwZGF0ZSB0aGUgcmFuZ2UgW2wsIHJdIGluIEEKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgewogICAgICAgICAgICAgICAgICAgIHVwZGF0ZSgxLCAxLCBOLCBpLCBjKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIC8vIFVwZGF0ZSB0aGUgcmFuZ2UgW1BbbF0sIFBbcl1dIGluIEEKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgewogICAgICAgICAgICAgICAgICAgIHVwZGF0ZSgxLCAxLCBOLCBQX21hcFtpXSwgYyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHR5cGUgPT0gMiB8fCB0eXBlID09IDMpIHsKICAgICAgICAgICAgaW50IGwsIHI7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHI7CgogICAgICAgICAgICBpZiAodHlwZSA9PSAyKSB7CiAgICAgICAgICAgICAgICAvLyBRdWVyeSB0aGUgc3VtIG9mIHRoZSByYW5nZSBbbCwgcl0gaW4gQQogICAgICAgICAgICAgICAgY291dCA8PCBxdWVyeSgxLCAxLCBOLCBsLCByKSA8PCBlbmRsOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgLy8gUXVlcnkgdGhlIHN1bSBvZiB0aGUgcmFuZ2UgW1BbbF0sIFBbcl1dIGluIEEKICAgICAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgc3VtICs9IHF1ZXJ5KDEsIDEsIE4sIFBfbWFwW2ldLCBQX21hcFtpXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3V0IDw8IHN1bSA8PCBlbmRsOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAwOwp9