import bisect
import sys
L = []
def insertValue(x):
pos = bisect.bisect_left(L, x)
if pos >= len(L) or L[pos] != x:
L.insert(pos, x)
def eraseValue(x):
pos = bisect.bisect_left(L, x)
if pos < len(L) and L[pos] == x:
L.pop(pos)
def xInSet(x):
pos = bisect.bisect_left(L, x)
print("YES" if pos < len(L) and L[pos] == x else "NO")
def sizeOfSet():
print(len(L))
def getPre(x):
pos = bisect.bisect_left(L, x)
print(L[pos - 1] if pos > 0 else -1)
def getBack(x):
pos = bisect.bisect_right(L, x)
print(L[pos] if pos < len(L) else -1)
def main():
q = int(sys.stdin.readline())
for _ in range(q):
parts = list(map(int, sys.stdin.readline().split()))
op = parts[0]
x = parts[1] if len(parts) > 1 else 0
if op == 1:
insertValue(x)
elif op == 2:
eraseValue(x)
elif op == 3:
xInSet(x)
elif op == 4:
sizeOfSet()
elif op == 5:
getPre(x)
elif op == 6:
getBack(x)
if __name__ == "__main__":
main()
这题必须用bisect才能达到时间要求O(nlogn), 本来用set()很方便,但是O(n^2) 用例7超时. 哎 Python。。。
然后用input()也不行,一定要sys.stdin.readline()

京公网安备 11010502036488号