class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def update(self, index, delta):
while index <= self.size:
self.tree[index] += delta
index += index & -index
def query(self, index):
total = 0
while index > 0:
total += self.tree[index]
index -= index & -index
return total
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)
def process_queries(N, Q, permutation, queries):
fenwick = FenwickTree(N)
results = []
for query in queries:
T = query[0]
if T == 0: # Type 0 update
l, r, c = query[1], query[2], query[3]
fenwick.range_update(l, r, c)
elif T == 1: # Type 1 update
l, r, c = query[1], query[2], query[3]
fenwick.range_update(permutation[l - 1], permutation[r - 1], c)
elif T == 2: # Type 2 query
l, r = query[1], query[2]
result = fenwick.range_query(l, r)
results.append(result)
elif T == 3: # Type 3 query
l, r = query[1], query[2]
result = fenwick.range_query(permutation[l - 1], permutation[r - 1])
results.append(result)
return results
def main():
import sys
input = sys.stdin.read
data = input().strip().splitlines()
first_line = data[0].split()
N = int(first_line[0])
Q = int(first_line[1])
permutation = list(map(int, data[1].split()))
queries = []
for i in range(2, 2 + Q):
queries.append(list(map(int, data[i].split())))
results = process_queries(N, Q, permutation, queries)
# Output results for type 2 and 3 queries
for result in results:
print(result)
if __name__ == "__main__":
main()
Y2xhc3MgRmVud2lja1RyZWU6CiAgICBkZWYgX19pbml0X18oc2VsZiwgc2l6ZSk6CiAgICAgICAgc2VsZi5zaXplID0gc2l6ZQogICAgICAgIHNlbGYudHJlZSA9IFswXSAqIChzaXplICsgMSkKCiAgICBkZWYgdXBkYXRlKHNlbGYsIGluZGV4LCBkZWx0YSk6CiAgICAgICAgd2hpbGUgaW5kZXggPD0gc2VsZi5zaXplOgogICAgICAgICAgICBzZWxmLnRyZWVbaW5kZXhdICs9IGRlbHRhCiAgICAgICAgICAgIGluZGV4ICs9IGluZGV4ICYgLWluZGV4CgogICAgZGVmIHF1ZXJ5KHNlbGYsIGluZGV4KToKICAgICAgICB0b3RhbCA9IDAK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