#!/usr/bin/env python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% N, Q = map(int, readline().split()) A = [0] + list(map(int, readline().split())) m = map(int, read().split()) QX = tuple(zip(m, m)) # %% class BinaryIndexedTree(): def __init__(self, seq): self.size = len(seq) self.depth = self.size.bit_length() self.build(seq) def build(self, seq): data = seq size = self.size for i, x in enumerate(data): j = i + (i & (-i)) if j < size: data[j] += data[i] self.data = data def __repr__(self): return self.data.__repr__() def get_sum(self, i): data = self.data s = 0 while i: s += data[i] i -= i & -i return s def add(self, i, x): data = self.data size = self.size while i < size: data[i] += x i += i & -i def find_kth_element(self, k): data = self.data; size = self.size x, sx = 0, 0 dx = 1 << (self.depth) for i in range(self.depth - 1, -1, -1): dx = (1 << i) if x + dx >= size: continue y = x + dx sy = sx + data[y] if sy < k: x, sx = y, sy return x + 1 # %% value = BinaryIndexedTree(A) connection = [0] * (N + 1) right_end_count = BinaryIndexedTree([0] + [1] * N) for q, x in QX: if q == 1: if not connection[x]: connection[x] = 1 right_end_count.add(x, -1) elif q == 2: if connection[x]: connection[x] -= 1 right_end_count.add(x, 1) elif q == 3: value.add(x, 1) elif q == 4: k = right_end_count.get_sum(x - 1) if k == 0: L = 1 else: L = right_end_count.find_kth_element(k) + 1 R = right_end_count.find_kth_element(k + 1) S = value.get_sum(R) - value.get_sum(L - 1) print(S)