#include #include #include #include #include #include #include using namespace std; 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; } int sum(int k) const { int s = 0; for (; k > 0; k -= k & -k) s += b[k]; return s; } int lower_bound(int64_t s, const vector>& a) const { //0 <= a[k].first + d + bt.sum(k) - m; //0 <= bt.sum(k) - s; if (s - a[0].first <= 0) return 0; int x = 0, w = 1; while (w << 1 <= n) w <<= 1; do { int k = x + w; if (k <= n && b[k] < s - a[k].first) { s -= b[k]; x += w; } } while (w >>= 1); return x + 1; } vector b; int n; }; using I = int; template I lower_bound(I i0, I i1, F f) { while (i0 < i1) { I i = (i0 + i1) / 2; if (f(i)) i1 = i; else i0 = i + 1; } return i0; } int main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; vector t(q), x(q); vector> a; int64_t d = 0; for (int i = 0; i < q; i++) { cin >> t[i] >> x[i]; if (t[i] == 1) a.push_back(pair(x[i] - d, i)); if (t[i] == 2) x[i] = a[x[i] - 1].second; if (t[i] == 3) d += x[i]; } int n = a.size(); sort(a.begin(), a.end()); vector p(q, -1); for (int j = 0; j < n; j++) { p[a[j].second] = j; } BIT bt(n); d = 0; int m = 0; for (int i = 0; i < q; i++) { if (t[i] == 1) { bt.add(p[i], 1); m++; } else if (t[i] == 2) { bt.add(p[x[i]], -1); m--; } else { d += x[i]; } int k = bt.lower_bound(m - d, a); cout << (k < n ? m - bt.sum(k) : 0) << '\n'; } return 0; }