#include #include #include #include #include using namespace std; template struct SegmentTree { using OP = function; vector tree; int size; const OP f; // bin' operation const Monoid e; // neutral element SegmentTree(int nmemb, const Monoid &e, const OP &f): f(f), e(e) { size = 1; while (size < nmemb) { size *= 2; } tree.assign(2 * size - 1, e); } void update(int k, Monoid dat) { k += size - 1; tree[k] = dat; while(k > 0) { k = (k - 1) / 2; tree[k] = f(tree[2 * k + 1], tree[2 * k + 2]); } } // [l, r) Monoid query(int l, int r) { l += size - 1; r += size - 1; Monoid d = e; while (l < r) { if (l % 2 == 0) { d = f(d, tree[l]); l++; } if (r % 2 == 0) { r--; d = f(d, tree[r]); } l = (l - 1) / 2; r = (r - 1) / 2; } return d; } Monoid operator[] (const int k) { return query(k, k + 1); } }; // 車両がつながってる区間を考える // セグ木で、二分探索 区間の長さと総和は合う? int main() { using ll = long long; ll n, q; ll a; ll com, x; cin >> n >> q; SegmentTree connect(n + 1, 0, [](ll l, ll r){return l + r;}); SegmentTree sum_a(n + 1, 0, [](ll l, ll r){return l + r;}); for (int i = 0; i < n; i++) { cin >> a; sum_a.update(i, a); } while(q--) { cin >> com >> x; --x; if (com == 1) { connect.update(x, 1); } else if (com == 2) { connect.update(x, 0); } else if (com == 3) { sum_a.update(x, sum_a[x] + 1); } else if (com == 4) { ll valid, invalid, mid; ll l, r; // right valid = x; invalid = n + 1; while(invalid - valid > 1) { mid = (invalid + valid) / 2; if (connect.query(x, mid) >= mid - x) { valid = mid; } else { invalid = mid; } } r = valid + 1; // left valid = x; invalid = -1; while (valid - invalid > 1) { mid = (invalid + valid) / 2; if (connect.query(mid, x + 1) >= x - mid + 1) { valid = mid; } else { invalid = mid; } } l = valid; cout << sum_a.query(l, r) << endl; } } }