fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7.  
  8. int q;
  9. cin >> q;
  10. multiset<int> s;
  11. while (q--) {
  12. int type, x;
  13. cin >> type >> x;
  14. if (type == 1) {
  15. s.insert(x);
  16. } else if (type == 2) {
  17. // s.count(x): so lan xuat hien gia tri x hoac s.find(x): iterator tro toi phan tu x trong tap hop
  18. if(s.count(x) == 0) {
  19. continue;
  20. }
  21. // if (s.find(x) == s.end()) {
  22. // continue;
  23. // }
  24. s.erase(s.find(x)); // s.erase(x) se xoa toan bo gia tri x trong tap hop
  25. } else if (type == 3) {
  26. // lower_bound(x): tra ve iterator tro toi phan tu nho nhat >= x, neu kco tro toi end
  27. // upper_bound(x): tra ve iterator tro toi phan tu nho nhat > x, neu kco tro toi end
  28. auto pos = s.upper_bound(x);
  29. if (pos == s.end()) {
  30. cout << - 1 << '\n';
  31. } else {
  32. cout << *pos << '\n';
  33. }
  34. } else {
  35. auto pos = s.upper_bound(x);
  36. if (pos == s.begin()) {
  37. cout << - 1 << '\n';
  38. } else {
  39. pos--;
  40. cout << *pos << '\n';
  41. }
  42. }
  43. // s.size(): kich thuoc
  44. // s.empty(): tra ve true neu s rong
  45. // s.clear(): xoa toan bo s
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5324KB
stdin
8
4 5
1 2
1 3
1 2
3 2
1 5
2 3
4 4
stdout
-1
3
2