import sequtils,strutils type binaryIndexTree[I:static[int]] = array[I + 1,int] # proc add(BIT :var binaryIndexTree,i : int, a : int)= var index = i while BIT[0] >= index: BIT[index] += a index += ((index xor (index - 1)) and index) proc sum(BIT : binaryIndexTree, i : int):int = var index = i while index > 0: result += BIT[index] index = (index and (index - 1)) proc initBit(i : static[int]) : binaryIndexTree[i] = var res : binaryIndexTree[i] res[0] = i return res var N, Q, k, x : int (N, Q) = stdin.readline.split.map(parseInt) var BITf = initBit(100010) BITa = initBit(100010) F : array[100010, bool] A = @[0] & stdin.readline.split.map(parseInt) for i in 1..N: BITf.add(i, 1) BITa.add(i, A[i]) for q in 1..Q: (k, x) = stdin.readline.split.map(parseInt) if k == 1 and F[x + 1] == false: F[x + 1] = true BITf.add(x + 1, -1) elif k == 2 and F[x + 1] == true: F[x + 1] = false BITf.add(x + 1, 1) elif k == 3: BITa.add(x, 1) elif k == 4: var m = 1 var M = x var l, r : int if BITf.sum(1) == BITf.sum(x): M = 1 while M - m > 1: var mid = (M + m) div 2 if BITf.sum(mid) == BITf.sum(x): M = mid else: m = mid l = M m = x M = N if BITf.sum(x) == BITf.sum(N): m = N while M - m > 1: var mid = (M + m) div 2 if BITf.sum(mid) == BITf.sum(x): m = mid else: M = mid r = m echo BITa.sum(r) - BITa.sum(l - 1)