class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def update(self, idx, delta):
while idx <= self.size:
self.tree[idx] += delta
idx += idx & -idx
def query(self, idx):
result = 0
while idx > 0:
result += self.tree[idx]
idx -= idx & -idx
return result
def range_update(self, left, right, delta):
self.update(left, delta)
self.update(right + 1, -delta)
def range_query(self, left, right):
return self.query(right) - self.query(left - 1)
# Read input
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
Q = int(data[1])
# Read permutation P
P = list(map(int, data[2:2 + N]))
# Initialize Fenwick Trees for original indices and permutation indices
original_tree = FenwickTree(N)
permutation_tree = FenwickTree(N)
output = []
idx = 2 + N
for _ in range(Q):
T = int(data[idx])
if T == 0:
l = int(data[idx + 1])
r = int(data[idx + 2])
c = int(data[idx + 3])
original_tree.range_update(l, r, c)
idx += 4
elif T == 1:
l = int(data[idx + 1])
r = int(data[idx + 2])
c = int(data[idx + 3])
for i in range(l, r + 1):
permutation_tree.update(P[i - 1], c)
idx += 4
elif T == 2:
l = int(data[idx + 1])
r = int(data[idx + 2])
result = original_tree.range_query(l, r)
output.append(result)
idx += 3
elif T == 3:
l = int(data[idx + 1])
r = int(data[idx + 2])
result = 0
for i in range(l, r + 1):
result += permutation_tree.query(P[i - 1]) - permutation_tree.query(P[i - 1] - 1)
output.append(result)
idx += 3
# Print all results for the queries
print("\n".join(map(str, output)))
Y2xhc3MgRmVud2lja1RyZWU6CiAgICBkZWYgX19pbml0X18oc2VsZiwgc2l6ZSk6CiAgICAgICAgc2VsZi5zaXplID0gc2l6ZQogICAgICAgIHNlbGYudHJlZSA9IFswXSAqIChzaXplICsgMSkKCiAgICBkZWYgdXBkYXRlKHNlbGYsIGlkeCwgZGVsdGEpOgogICAgICAgIHdoaWxlIGlkeCA8PSBzZWxmLnNpemU6CiAgICAgICAgICAgIHNlbGYudHJlZVtpZHhdICs9IGRlbHRhCiAgICAgICAgICAgIGlkeCArPSBpZHggJiAtaWR4CgogICAgZGVmIHF1ZXJ5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kYXRhW2lkeCArIDNdKQogICAgICAgIG9yaWdpbmFsX3RyZWUucmFuZ2VfdXBkYXRlKGwsIHIsIGMpCiAgICAgICAgaWR4ICs9IDQKICAgIGVsaWYgVCA9PSAxOgogICAgICAgIGwgPSBpbnQoZGF0YVtpZHggKyAxXSkKICAgICAgICByID0gaW50KGRhdGFbaWR4ICsgMl0pCiAgICAgICAgYyA9IGludChkYXRhW2lkeCArIDNdKQogICAgICAgIGZvciBpIGluIHJhbmdlKGwsIHIgKyAxKToKICAgICAgICAgICAgcGVybXV0YXRpb25fdHJlZS51cGRhdGUoUFtpIC0gMV0sIGMpCiAgICAgICAgaWR4ICs9IDQKICAgIGVsaWYgVCA9PSAyOgogICAgICAgIGwgPSBpbnQoZGF0YVtpZHggKyAxXSkKICAgICAgICByID0gaW50KGRhdGFbaWR4ICsgMl0pCiAgICAgICAgcmVzdWx0ID0gb3JpZ2luYWxfdHJlZS5yYW5nZV9xdWVyeShsLCByKQogICAgICAgIG91dHB1dC5hcHBlbmQocmVzdWx0KQogICAgICAgIGlkeCArPSAzCiAgICBlbGlmIFQgPT0gMzoKICAgICAgICBsID0gaW50KGRhdGFbaWR4ICsgMV0pCiAgICAgICAgciA9IGludChkYXRhW2lkeCArIDJdKQogICAgICAgIHJlc3VsdCA9IDAKICAgICAgICBmb3IgaSBpbiByYW5nZShsLCByICsgMSk6CiAgICAgICAgICAgIHJlc3VsdCArPSBwZXJtdXRhdGlvbl90cmVlLnF1ZXJ5KFBbaSAtIDFdKSAtIHBlcm11dGF0aW9uX3RyZWUucXVlcnkoUFtpIC0gMV0gLSAxKQogICAgICAgIG91dHB1dC5hcHBlbmQocmVzdWx0KQogICAgICAgIGlkeCArPSAzCgojIFByaW50IGFsbCByZXN1bHRzIGZvciB0aGUgcXVlcmllcwpwcmludCgiXG4iLmpvaW4obWFwKHN0ciwgb3V0cHV0KSkpCg==