import bisect
# S 用于 O(1) 的存在性检查和快速删除
S = set()
# sort_list 用于 O(log n) 的前驱和后继查询
# 我们将确保它始终是有序的
sort_list = []
def insertValue(x):
# TODO: 实现插入逻辑
if x not in S:
S.add(x)
# 使用 bisect.insort 来保持列表有序
bisect.insort(sort_list, x)
def eraseValue(x):
# TODO: 实现删除逻辑
if x in S:
S.discard(x)
# 找到元素在有序列表中的索引并删除
# 先检查元素是否存在,虽然上面已经判断过,但这是个好习惯
idx = bisect.bisect_left(sort_list, x)
if idx < len(sort_list) and sort_list[idx] == x:
sort_list.pop(idx)
def xInSet(x):
# TODO: 实现存在性检查
return x in S
def sizeOfSet():
# TODO: 返回集合大小
return len(S)
def getPre(x):
# TODO: 实现找前驱
# 前驱是指小于 x 的最大元素,x 不一定在集合中
idx = bisect.bisect_left(sort_list, x)
# 如果 idx == 0,说明所有元素都 >= x,没有前驱
if idx > 0:
return sort_list[idx - 1]
else:
return -1
def getBack(x):
# TODO: 实现找后继
# 后继是指大于 x 的最小元素,x 不一定在集合中
idx = bisect.bisect_right(sort_list, x)
# 如果 idx == len(sort_list),说明所有元素都 <= x,没有后继
if idx < len(sort_list):
return sort_list[idx]
else:
return -1
def main():
q = int(input())
for _ in range(q):
parts = list(map(int, input().split()))
op = parts[0]
if op in (1, 2, 3, 5, 6):
x = parts[1]
if op == 1:
insertValue(x)
elif op == 2:
eraseValue(x)
elif op == 3:
print("YES" if xInSet(x) else "NO")
elif op == 4:
print(sizeOfSet())
elif op == 5:
print(getPre(x))
elif op == 6:
print(getBack(x))
if __name__ == "__main__":
main()