#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) {
// ... (same as before)
}
void update(int node, int start, int end, int idx, int val) {
// ... (same as before)
}
int query(int node, int start, int end, int l, int r) {
// ... (same as before)
}
int main() {
int N, Q;
cin >> N >> Q;
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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBOID0gMWU1ICsgNTsKCmludCBBW05dLCBQW05dLCBQX21hcFtOXTsKdmVjdG9yPGludD4gc2VnVHJlZSg0ICogTik7Cgp2b2lkIGJ1aWxkKGludCBub2RlLCBpbnQgc3RhcnQsIGludCBlbmQpIHsKICAgIC8vIC4uLiAoc2FtZSBhcyBiZWZvcmUpCn0KCnZvaWQgdXBkYXRlKGludCBub2RlLCBpbnQgc3RhcnQsIGludCBlbmQsIGludCBpZHgsIGludCB2YWwpIHsKICAgIC8vIC4uLiAoc2FtZSBhcyBiZWZvcmUpCn0KCmludCBxdWVyeShpbnQgbm9kZSwgaW50IHN0YXJ0LCBpbnQgZW5kLCBpbnQgbCwgaW50IHIpIHsKICAgIC8vIC4uLiAoc2FtZSBhcyBiZWZvcmUpCn0KCmludCBtYWluKCkgewogICAgaW50IE4sIFE7CiAgICBjaW4gPj4gTiA+PiBROwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gTjsgaSsrKSB7CiAgICAgICAgY2luID4+IFBbaV07CiAgICAgICAgUF9tYXBbUFtpXV0gPSBpOwogICAgfQoKICAgIC8vIEJ1aWxkIHRoZSBTZWdtZW50IFRyZWUKICAgIGJ1aWxkKDEsIDEsIE4pOwoKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBpbnQgdHlwZTsKICAgICAgICBjaW4gPj4gdHlwZTsKCiAgICAgICAgaWYgKHR5cGUgPT0gMCB8fCB0eXBlID09IDEpIHsKICAgICAgICAgICAgaW50IGwsIHIsIGM7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHIgPj4gYzsKCiAgICAgICAgICAgIGlmICh0eXBlID09IDApIHsKICAgICAgICAgICAgICAgIC8vIFVwZGF0ZSB0aGUgcmFuZ2UgW2wsIHJdIGluIEEKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgewogICAgICAgICAgICAgICAgICAgIHVwZGF0ZSgxLCAxLCBOLCBpLCBjKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIC8vIFVwZGF0ZSB0aGUgcmFuZ2UgW1BbbF0sIFBbcl1dIGluIEEKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgewogICAgICAgICAgICAgICAgICAgIHVwZGF0ZSgxLCAxLCBOLCBQX21hcFtpXSwgYyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHR5cGUgPT0gMiB8fCB0eXBlID09IDMpIHsKICAgICAgICAgICAgaW50IGwsIHI7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHI7CgogICAgICAgICAgICBpZiAodHlwZSA9PSAyKSB7CiAgICAgICAgICAgICAgICAvLyBRdWVyeSB0aGUgc3VtIG9mIHRoZSByYW5nZSBbbCwgcl0gaW4gQQogICAgICAgICAgICAgICAgY291dCA8PCBxdWVyeSgxLCAxLCBOLCBsLCByKSA8PCBlbmRsOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgLy8gUXVlcnkgdGhlIHN1bSBvZiB0aGUgcmFuZ2UgW1BbbF0sIFBbcl1dIGluIEEKICAgICAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgc3VtICs9IHF1ZXJ5KDEsIDEsIE4sIFBfbWFwW2ldLCBQX21hcFtpXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3V0IDw8IHN1bSA8PCBlbmRsOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAwOwp9