using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, q) = (c[0], c[1]); var a = NList; var query = NArr(q); var ft = new FenwickTree(n + 1); for (var i = 0; i < n; ++i) ft.Add(i + 1, a[i]); var tree = new AvlTree(); for (var i = 0; i < n; ++i) tree.Insert(new Range(i + 1, i + 1)); var ans = new List(); for (var i = 0; i < q; ++i) { var k = query[i][0]; var x = query[i][1]; var pos = 0; var rx = tree.LowerBound(new Range(x, x), ref pos); if (k == 1) { if (rx.Right == x) { var next = tree.ElementAt(pos + 1); tree.Delete(rx); tree.Delete(next); tree.Insert(new Range(rx.Left, next.Right)); } } else if (k == 2) { if (rx.Right > x) { tree.Delete(rx); tree.Insert(new Range(rx.Left, x)); tree.Insert(new Range(x + 1, rx.Right)); } } else if (k == 3) { ft.Add(x, 1); } else { ans.Add(ft.Sum(rx.Right) - ft.Sum(rx.Left - 1)); } } WriteLine(string.Join("\n", ans)); } class Range : IComparable { public int Left; public int Right; public Range(int l, int r) { Left = l; Right = r; } public int CompareTo(Range b) { return Right.CompareTo(b.Right); } } public class AvlTree where T: IComparable { private class Node where U: IComparable { public U Value; public Node Lc = null; public Node Rc = null; public int Height; public int Count; public int CCount; public Node(int height, U value) { Height = height; Count = 1; CCount = 1; Value = value; } } private Node root = null; static int GetHeight(Node t) { return t == null ? 0 : t.Height; } static int GetCount(Node t) { return t == null ? 0 : t.Count; } static int GetBalance(Node t) { return GetHeight(t.Lc) - GetHeight(t.Rc); } static void Recalc(Node t) { if (t == null) return; t.Height = 1 + Math.Max(GetHeight(t.Lc), GetHeight(t.Rc)); t.Count = t.CCount + GetCount(t.Lc) + GetCount(t.Rc); } static Node RotateLeft(Node t) { Node u = t.Rc; Node t2 = u.Lc; u.Lc = t; t.Rc = t2; Recalc(t); Recalc(u); return u; } static Node RotateRight(Node t) { Node u = t.Lc; Node t2 = u.Rc; u.Rc = t; t.Lc = t2; Recalc(t); Recalc(u); return u; } static Node RotateLR(Node t) { t.Lc = RotateLeft(t.Lc); return RotateRight(t); } static Node RotateRL(Node t) { t.Rc = RotateRight(t.Rc); return RotateLeft(t); } static Node BalanceLeft(Node t) { if (GetBalance(t) > 1) { if (GetBalance(t.Lc) < 0) t = RotateLR(t); else t = RotateRight(t); } Recalc(t); return t; } static Node BalanceRight(Node t) { if (GetBalance(t) < -1) { if (GetBalance(t.Rc) > 0) t = RotateRL(t); else t = RotateLeft(t); } Recalc(t); return t; } /// valueを挿入する public void Insert(T value) { root = Insert(root, value); } Node Insert(Node cur, T value) { if (cur == null) { return new Node(1, value); } var d = value.CompareTo(cur.Value); if (d < 0) { cur.Lc = Insert(cur.Lc, value); return BalanceLeft(cur); } else if (d > 0) { cur.Rc = Insert(cur.Rc, value); return BalanceRight(cur); } else { ++cur.CCount; Recalc(cur); return cur; } } /// valueを削除する 存在しない場合はなにもしない public void Delete(T value) { root = Delete(root, value); } Node Delete(Node cur, T value) { if (cur == null) return null; var d = value.CompareTo(cur.Value); if (d < 0) { cur.Lc = Delete(cur.Lc, value); return BalanceRight(cur); } else if (d > 0) { cur.Rc = Delete(cur.Rc, value); return BalanceLeft(cur); } else { --cur.CCount; if (cur.CCount == 0) { if (cur.Lc != null) { Node del = null; var max = DeleteMax(cur.Lc, ref del); cur.Lc = max; cur.Value = del.Value; cur.CCount = del.CCount; return BalanceRight(cur); } else { return cur.Rc; } } else { Recalc(cur); return cur; } } } Node DeleteMax(Node t, ref Node del) { if (t.Rc == null) { del = t; return t.Lc; } else { var max = DeleteMax(t.Rc, ref del); t.Rc = max; return BalanceLeft(t); } } /// 小さいほうからpos番目の要素を取得する public T ElementAt(int pos) { if (pos < 0 || pos >= GetCount(root)) return default; var t = ElementAt(root, pos); return t.Value; } /// 小さいほうからpos番目の要素を取得する public T ElementAt(int pos, T defaultValue) { if (pos < 0 || pos >= GetCount(root)) return defaultValue; return ElementAt(pos); } Node ElementAt(Node cur, int pos) { if (pos < GetCount(cur.Lc)) return ElementAt(cur.Lc, pos); if (pos < GetCount(cur.Lc) + cur.CCount) return cur; if (pos < cur.Count) return ElementAt(cur.Rc, pos - GetCount(cur.Lc) - cur.CCount); return null; } public void Debug() { if (root == null) return; Console.WriteLine($"root val:{root.Value}, CCount:{root.CCount}, Count:{root.Count}"); Debug(root.Value, root.Lc); Debug(root.Value, root.Rc); } void Debug(T pval, Node cur) { if (cur == null) return; Console.WriteLine($"{pval} -> val:{cur.Value}, CCount:{cur.CCount}, Count:{cur.Count}"); Debug(cur.Value, cur.Lc); Debug(cur.Value, cur.Rc); } /// value以上でもっとも小さい値とその場所を返却する そのようなものが存在しなければデフォルト値を返す public T LowerBound(T value, ref int index) { var cur = root; T ans = default; var add = 0; index = GetCount(root); while (cur != null) { if (value.CompareTo(cur.Value) <= 0) { ans = cur.Value; index = add + GetCount(cur.Lc); cur = cur.Lc; } else { add += GetCount(cur.Lc) + cur.CCount; cur = cur.Rc; } } return ans; } /// 要素の個数を求める public int GetCount() { return GetCount(root); } /// 要素の一覧を返す public List GetValues() { var list = new List(); if (root != null) GetValues(root, list); return list; } static void GetValues(Node cur, List list) { if (cur.Lc != null) GetValues(cur.Lc, list); list.Add(cur.Value); if (cur.Rc != null) GetValues(cur.Rc, list); } /// 指定された要素が存在するか求める /// /// public bool Contains(T value) { if (root == null) return false; return Contains(root, value); } static bool Contains(Node cur, T value) { var d = cur.Value.CompareTo(value); if (d == 0) return true; if (d > 0) { if (cur.Lc == null) return false; return Contains(cur.Lc, value); } else { if (cur.Rc == null) return false; return Contains(cur.Rc, value); } } } class FenwickTree { int size; long[] tree; public FenwickTree(int size) { this.size = size; tree = new long[size + 2]; } public void Add(int index, long value) { ++index; for (var x = index; x <= size; x += (x & -x)) tree[x] += value; } /// 先頭からindexまでの和(include index) public long Sum(int index) { if (index < 0) return 0; ++index; var sum = 0L; for (var x = index; x > 0; x -= (x & -x)) sum += tree[x]; return sum; } /// 区間[left, right)の和 public long Range(int linc, int rexc) { if (linc == 0) return Sum(rexc - 1); return Sum(rexc - 1) - Sum(linc - 1); } public long Get(int index) { if (index == 0) return Sum(0); return Sum(index) - Sum(index - 1); } /// Sum(x) >= value となる最小のxを求める // 各要素は非負であること public int LowerBound(long value) { if (value < 0) return -1; var x = 0; var b = 1; while (b * 2 <= size) b <<= 1; for (var k = b; k > 0; k >>= 1) { if (x + k <= size && tree[x + k] < value) { value -= tree[x + k]; x += k; } } return x; } public long[] Debug() { var ans = new long[size]; for (var i = 0; i < size; ++i) ans[i] = Get(i); return ans; } } }