You are given an array A(1-based index) consisting of N integers. You have to process two types of queries on this array.
Type 1: Given ID and VAL, perform the operation UPDATE(ID, VAL)
UPDATE(ID, VAL):
if VAL == 0:
return
AID = AID + VAL
if ID == N:
UPDATE(1, VAL - 1)
else :
UPDATE(ID + 1, VAL - 1)
Type 2: Given L and R, find QUERY(L, R)
QUERY(L, R):
if L == R:
return AL
if L == N:
return AL + QUERY(1, R)
else :
return AL + QUERY(L + 1, R)
Input:
Output:
Constraints:
Initial Array A[] = {4, 1, 2, 3, 9, 1}
1st query: 1 + 2 + 3 + 9 = 15
2nd query: Updated Array A[] = {6, 2, 2, 8, 13, 4}
3rd query: 4
4th query: Updated Array A[] = {6, 6, 5, 10, 14, 4}
5th query: 5 + 10 + 14 + 4 + 6 = 39