class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def add(self, index, value):
while index <= self.size:
self.tree[index] += value
index += index & -index
def sum(self, index):
total = 0
while index > 0:
total += self.tree[index]
index -= index & -index
return total
def range_add(self, left, right, value):
self.add(left, value)
self.add(right + 1, -value)
def range_sum(self, left, right):
return self.sum(right) - self.sum(left - 1)
import sys
def main():
input = sys.stdin.read
data = input().strip().split()
index = 0
N = int(data[index])
Q = int(data[index + 1])
index += 2
P = list(map(int, data[index:index + N]))
index += N
# Create Fenwick Tree for handling updates and queries
fenwick = FenwickTree(N)
result = []
for _ in range(Q):
T = int(data[index])
if T == 0:
l = int(data[index + 1])
r = int(data[index + 2])
c = int(data[index + 3])
fenwick.range_add(l, r, c)
index += 4
elif T == 1:
l = int(data[index + 1])
r = int(data[index + 2])
c = int(data[index + 3])
fenwick.range_add(P[l - 1], P[r - 1], c) # use P to index
index += 4
elif T == 2:
l = int(data[index + 1])
r = int(data[index + 2])
result.append(fenwick.range_sum(l, r))
index += 3
elif T == 3:
l = int(data[index + 1])
r = int(data[index + 2])
result.append(fenwick.range_sum(P[l - 1], P[r - 1])) # use P to index
index += 3
# Print results for all queries of type 2 and 3
sys.stdout.write('\n'.join(map(str, result)) + '\n')
if __name__ == "__main__":
main()
Y2xhc3MgRmVud2lja1RyZWU6CiAgICBkZWYgX19pbml0X18oc2VsZiwgc2l6ZSk6CiAgICAgICAgc2VsZi5zaXplID0gc2l6ZQogICAgICAgIHNlbGYudHJlZSA9IFswXSAqIChzaXplICsgMSkKCiAgICBkZWYgYWRkKHNlbGYsIGluZGV4LCB2YWx1ZSk6CiAgICAgICAgd2hpbGUgaW5kZXggPD0gc2VsZi5zaXplOgogICAgICAgICAgICBzZWxmLnRyZWVbaW5kZXhdICs9IHZhbHVlCiAgICAgICAgICAgIGluZGV4ICs9IGluZGV4ICYgLWluZGV4CgogICAgZGVmIHN1bShzZWxmLCBpbmRleCk6CiAgICAgICAgdG90YWwgPSAwCiAgICAgICAgd2hpbGUgaW5kZXggPiAwOgogICAgICAgICAgICB0b3RhbCArPSBzZWxmLnRyZWVbaW5kZXhdCiAgICAgICAgICAgIGluZGV4IC09IGluZGV4ICYgLWluZGV4CiAgICAgICAgcmV0dXJuIHRvdGFsCgogICAgZGVmIHJhbmdlX2FkZChzZWxmLCBsZWZ0LCByaWdodCwgdmFsdWUpOgogICAgICAgIHNlbGYuYWRkKGxlZnQsIHZhbHVlKQogICAgICAgIHNlbGYuYWRkKHJpZ2h0ICsgMSwgLXZhbHVlKQoKICAgIGRlZiByYW5nZV9zdW0oc2VsZiwgbGVmdCwgcmlnaHQpOgogICAgICAgIHJldHVybiBzZWxmLnN1bShyaWdodCkgLSBzZWxmLnN1bShsZWZ0IC0gMSkKCmltcG9ydCBzeXMKCmRlZiBtYWluKCk6CiAgICBpbnB1dCA9IHN5cy5zdGRpbi5yZWFkCiAgICBkYXRhID0gaW5wdXQoKS5zdHJpcCgpLnNwbGl0KCkKICAgIAogICAgaW5kZXggPSAwCiAgICBOID0gaW50KGRhdGFbaW5kZXhdKQogICAgUSA9IGludChkYXRhW2luZGV4ICsgMV0pCiAgICBpbmRleCArPSAyCgogICAgUCA9IGxpc3QobWFwKGludCwgZGF0YVtpbmRleDppbmRleCArIE5dKSkKICAgIGluZGV4ICs9IE4KCiAgICAjIENyZWF0ZSBGZW53aWNrIFRyZWUgZm9yIGhhbmRsaW5nIHVwZGF0ZXMgYW5kIHF1ZXJpZXMKICAgIGZlbndpY2sgPSBGZW53aWNrVHJlZShOKQoKICAgIHJlc3VsdCA9IFtdCiAgICBmb3IgXyBpbiByYW5nZShRKToKICAgICAgICBUID0gaW50KGRhdGFbaW5kZXhdKQogICAgICAgIGlmIFQgPT0gMDoKICAgICAgICAgICAgbCA9IGludChkYXRhW2luZGV4ICsgMV0pCiAgICAgICAgICAgIHIgPSBpbnQoZGF0YVtpbmRleCArIDJdKQogICAgICAgICAgICBjID0gaW50KGRhdGFbaW5kZXggKyAzXSkKICAgICAgICAgICAgZmVud2ljay5yYW5nZV9hZGQobCwgciwgYykKICAgICAgICAgICAgaW5kZXggKz0gNAogICAgICAgIGVsaWYgVCA9PSAxOgogICAgICAgICAgICBsID0gaW50KGRhdGFbaW5kZXggKyAxXSkKICAgICAgICAgICAgciA9IGludChkYXRhW2luZGV4ICsgMl0pCiAgICAgICAgICAgIGMgPSBpbnQoZGF0YVtpbmRleCArIDNdKQogICAgICAgICAgICBmZW53aWNrLnJhbmdlX2FkZChQW2wgLSAxXSwgUFtyIC0gMV0sIGMpICAjIHVzZSBQIHRvIGluZGV4CiAgICAgICAgICAgIGluZGV4ICs9IDQKICAgICAgICBlbGlmIFQgPT0gMjoKICAgICAgICAgICAgbCA9IGludChkYXRhW2luZGV4ICsgMV0pCiAgICAgICAgICAgIHIgPSBpbnQoZGF0YVtpbmRleCArIDJdKQogICAgICAgICAgICByZXN1bHQuYXBwZW5kKGZlbndpY2sucmFuZ2Vfc3VtKGwsIHIpKQogICAgICAgICAgICBpbmRleCArPSAzCiAgICAgICAgZWxpZiBUID09IDM6CiAgICAgICAgICAgIGwgPSBpbnQoZGF0YVtpbmRleCArIDFdKQogICAgICAgICAgICByID0gaW50KGRhdGFbaW5kZXggKyAyXSkKICAgICAgICAgICAgcmVzdWx0LmFwcGVuZChmZW53aWNrLnJhbmdlX3N1bShQW2wgLSAxXSwgUFtyIC0gMV0pKSAgIyB1c2UgUCB0byBpbmRleAogICAgICAgICAgICBpbmRleCArPSAzCgogICAgIyBQcmludCByZXN1bHRzIGZvciBhbGwgcXVlcmllcyBvZiB0eXBlIDIgYW5kIDMKICAgIHN5cy5zdGRvdXQud3JpdGUoJ1xuJy5qb2luKG1hcChzdHIsIHJlc3VsdCkpICsgJ1xuJykKCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBtYWluKCk=