fork(2) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct cmp {
  6. bool operator () (const int &u, const int &v) const {
  7. return u < v;
  8. }
  9. };
  10.  
  11. int n, m, x;
  12. set<int,cmp> s;
  13. set<int,cmp>::iterator it;
  14.  
  15. int main()
  16. {
  17. //freopen("INP.INP","r",stdin);
  18. //freopen("OUT.OUT","w",stdout);
  19.  
  20. while (scanf("%d", &n) == 1) {
  21. switch (n) {
  22. case 0 :
  23. return 0;
  24. case 1 :
  25. scanf("%d", &x);
  26. s.insert(x);
  27. break;
  28. case 2 :
  29. scanf("%d", &x);
  30. s.erase(x);
  31. break;
  32. case 3 :
  33. if (s.size() == 0) {printf("empty\n"); continue;}
  34. printf("%d\n", *(s.begin()));
  35. break;
  36. case 4 :
  37. if (s.size() == 0) {printf("empty\n"); continue;}
  38. printf("%d\n", *(s.rbegin()));
  39. break;
  40. case 5 :
  41. scanf("%d", &x);
  42. if (s.size() == 0) {printf("empty\n"); continue;}
  43. it = s.upper_bound(x);
  44. if (it == s.end()) printf("no\n"); else printf("%d\n", *(it));
  45. break;
  46. case 6 :
  47. scanf("%d", &x);
  48. if (s.size() == 0) {printf("empty\n"); continue;}
  49. it = s.lower_bound(x);
  50. if (it == s.end()) printf("no\n"); else printf("%d\n", *(it));
  51. break;
  52. case 7 :
  53. scanf("%d", &x);
  54. if (s.size() == 0) {printf("empty\n"); continue;}
  55. it = s.lower_bound(x);
  56. if (it == s.begin()) printf("no\n"); else printf("%d\n", *(--it));
  57. break;
  58. case 8 :
  59. scanf("%d", &x);
  60. if (s.size() == 0) {printf("empty\n"); continue;}
  61. it = s.lower_bound(x);
  62. if (*(it) == x) printf("%d\n", x);
  63. else if (it == s.begin()) printf("no\n");
  64. else printf("%d\n", *(--it));
  65. break;
  66. }
  67. }
  68.  
  69. return 0;
  70. }
  71.  
  72.  
Success #stdin #stdout 0s 3236KB
stdin
4
1 10
1 5
3
1 7
4
2 5
3
5 10
6 10
7 10
8 10
2 10
7 100
0
stdout
empty
5
10
7
no
10
7
10
7