#include #include #include #include #include #include #include #include using namespace std; using ll = long long; struct BIT { BIT(int n) : b(n + 1), n(n) {} void add(int i, int v) { for (int k = i + 1; k <= n; k += k & -k) b[k] += v; } ll sum(int k) { ll s = 0; for (; k > 0; k -= k & -k) s += b[k]; return s; } vector b; int n; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; BIT bt(n); map mp; for (int i = 0; i < n; i++) { int a; cin >> a; bt.add(i, a); mp.insert(mp.end(), make_pair(i, i + 1)); } for (int h = 0; h < q; h++) { int t, x; cin >> t >> x; x--; if (t == 1) { auto it = mp.upper_bound(x); it--; if (x + 1 < it->second) continue; int i0 = it->first; it = mp.erase(it); int i1 = it->second; it = mp.erase(it); mp.insert(it, make_pair(i0, i1)); } else if (t == 2) { auto it = mp.upper_bound(x); it--; if (x + 1 >= it->second) continue; int i0 = it->first; int i1 = it->second; it = mp.erase(it); mp.insert(it, make_pair(i0, x + 1)); mp.insert(it, make_pair(x + 1, i1)); } else if (t == 3) { bt.add(x, 1); } else if (t == 4) { auto it = mp.upper_bound(x); it--; cout << bt.sum(it->second) - bt.sum(it->first) << '\n'; } } return 0; }