#include using namespace std; template struct BIT{ int size; vector bit; BIT(vector &src) : size(src.size()), bit(src) { for (int k = 0; k < size - 1; ++k) bit[k | (k + 1)] += bit[k]; } T range_sum(int i, int j) {return sum(j) - sum(i - 1);} // return sum of v[i] to v[j] including both ends. T sum(int j){ T s = 0; for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j]; return s; } void add(int k, T a) {for (; k < size; k |= k + 1) bit[k] += a;} }; template struct BITrange{ int size; vector bit; BITrange(int n) : size(n), bit(n, 0){} T range_sum(int i, int j) {return sum(j - 1) - sum(i - 1);} // return sum of v[i] to v[j - 1]. T sum(int j){ T s = 0; for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j]; return s; } void add(int k, T a) {for (; k < size; k |= k + 1) bit[k] += a;} int queryR(int k){ auto sum_to_k_minus_k = this->sum(k - 1) - k; if (this->sum(size - 1) == size + sum_to_k_minus_k) return size; int OK = k; int NG = size; while (OK + 1 < NG){ int mid = (OK + NG) / 2; if (this->sum(mid - 1) == mid + sum_to_k_minus_k){ OK = mid; }else{ NG = mid; } } return OK; } int queryL(int k){ auto tmp = k - this->sum(k - 1); if (tmp == 0) return 0; int OK = k; int NG = 0; while (NG + 1 < OK){ int mid = (OK + NG) / 2; if (mid - this->sum(mid - 1) == tmp){ OK = mid; }else{ NG = mid; } } return OK; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; vector As(N, 0LL); for (int i = 0; i < N; ++i) cin >> As[i]; BIT bit(As); BITrange bitr(N); while (Q--){ int q, x; cin >> q >> x; if (q == 1){ if (bitr.range_sum(x - 1, x) == 0){ bitr.add(x - 1, 1); } }else if (q == 2){ if (bitr.range_sum(x - 1, x) == 1){ bitr.add(x - 1, -1); } }else if (q == 3){ bit.add(x - 1, 1LL); }else if (q == 4){ int R = bitr.queryR(x - 1); int L = bitr.queryL(x - 1); cout << bit.range_sum(L, R) << '\n'; } } return 0; }