fork download
  1. from collections import OrderedDict
  2. N, Q = map(int,input().split())
  3. arr=[x for x in input().split()]
  4. arr.reverse()
  5. mp = OrderedDict()
  6. for x in arr:
  7. mp[x]=1
  8. for q in range(Q):
  9. q=input().split()
  10. ty=int(q[0])
  11. if ty != 3:
  12. L=q[1]
  13. if ty == 1:
  14. if L in mp:
  15. print(-1)
  16. else:
  17. mp[L]=1
  18. elif ty == 2:
  19. if L not in mp:
  20. print(-1)
  21. else:
  22. del mp[L]
  23. else:
  24. try:
  25. L=mp.popitem(last=False)
  26. print(L[0])
  27. except:
  28. print(-1)
  29.  
Success #stdin #stdout 0.02s 9304KB
stdin
3 5
7 5 1
1 2
2 5
3
2 7
3
stdout
1
2