#!/usr/bin/env python import sys import math if sys.version_info[0] == 2: range, input = xrange, raw_input class FenwickTree: def __init__(self, a_list): # 0-indexed self.N = len(a_list) self.bit = a_list[:] for _ in range(self.N, 1 << int(math.ceil(math.log(self.N, 2)))): self.bit.append(0) for i in range(self.N-1): self.bit[i | (i+1)] += self.bit[i] def add(self, i, val): while i < self.N: self.bit[i] += val i |= i + 1 def sum(self, n): """[0, n)""" ret = 0 n -= 1 while n >= 0: ret += self.bit[n] n = (n & (n + 1)) - 1 return ret def index(self, k): return self.sum(k + 1) - self.sum(k) def count_index(self, k): left, right = -1, self.N - 1 while left + 1 < right: mid = (left + right) >> 1 if self.sum(mid + 1) >= k: right = mid else: left = mid return right def solve(Q, events): up_events = [0] * (Q + 1) for i, (x, y) in enumerate(events): if x == 3: up_events[i + 1] = y up_events[i + 1] += up_events[i] final_powers = [] for i, (x, y) in enumerate(events): if x == 1: final_power = y + up_events[Q] - up_events[i] final_powers.append((final_power, i)) final_powers.sort() N = len(final_powers) # print(final_powers) idx2fwt = [-1] * Q for i, (_, idx) in enumerate(final_powers): idx2fwt[idx] = i get_into_labo = [] in_labo = FenwickTree([0] * N) for i, (x, y) in enumerate(events): if x == 1: # assert(-10 ** 9 <= y <= 10 ** 9) # assert(idx2fwt[i] != -1) # assert(in_labo.index(idx2fwt[i]) == 0) in_labo.add(idx2fwt[i], 1) get_into_labo.append(i) elif x == 2: # assert(1 <= y < i + 1) # assert(in_labo.index(idx2fwt[get_into_labo[y - 1]]) == 1) in_labo.add(idx2fwt[get_into_labo[y - 1]], -1) elif x == 3: # assert(-10 ** 9 <= y <= 10 ** 9) pass else: assert(False) pass # print([in_labo.index(i) for i in range(N)]) left, right = 0, in_labo.sum(N) + 1 while left + 1 < right: mid = (left + right) >> 1 idx = in_labo.count_index(in_labo.sum(N) - mid + 1) orig = final_powers[idx][1] now_power = events[orig][1] + up_events[i + 1] - up_events[orig] # print(left, mid, right, idx) if now_power >= mid: left = mid else: right = mid print(left) if __name__ == '__main__': Q = int(input()) # assert(1 <= Q <= 2 * 10 ** 5) events = [tuple(map(int, input().split())) for _ in range(Q)] solve(Q, events)