fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. set<int> a;
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(0);
  7. cout.tie(0);
  8. int n;
  9. cin>>n;
  10. int k=0;
  11. for(int i=1;i<=n;i++){
  12. int kind,x,minn=1000000050;
  13. cin>>kind;
  14. if(kind==1){
  15. int l;
  16. cin>>l;
  17. if(a.find(l)!=a.end()){
  18. cout<<"Already Exist\n";
  19. }
  20. a.insert(l);
  21. }else if(kind==2){
  22. cin>>x;
  23. if(a.empty()){
  24. cout<<"Empty"<<"\n";
  25. continue;
  26. }
  27. auto it=a.upper_bound(x);
  28. if(it==a.begin()){
  29. cout<<*it<<"\n";
  30. a.erase(it);
  31. continue;
  32. }else if(it==a.end()){
  33. it--;
  34. cout<<*it<<"\n";
  35. a.erase(it);
  36. continue;
  37. }
  38. it--;
  39. auto it1=it;
  40. it1++;
  41. if(x-(*it)>(*it1)-x){
  42. cout<<*it1<<"\n";
  43. a.erase(it1);
  44. }else{
  45. cout<<*it<<"\n";
  46. a.erase(it);
  47. }
  48. }
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5312KB
stdin
7
1 1
1 5
1 3
2 3
2 3
2 3
2 3
stdout
3
1
5
Empty