fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5. #define int long long
  6.  
  7. const int N = 2e5, oo = 2e18, MOD = 1e9+7;
  8.  
  9.  
  10. void solve() {
  11. int q; cin >> q;
  12. map<int, int> fr;
  13. while (q--) {
  14. int t; cin >> t;
  15. if (t == 1) {
  16. int x; cin >> x;
  17. fr[x]++;
  18. } else if (t == 2) {
  19. int x, k; cin >> x >> k;
  20. if (fr.empty()) {
  21. cout << -1 << endl;
  22. continue;
  23. }
  24. auto it = fr.upper_bound(x);
  25. it--;
  26. bool found = false;
  27. for (auto i = it; ; i--) {
  28. auto [e, f] = *i;
  29. if (f >= k) {
  30. found = true;
  31. cout << e << endl;
  32. break;
  33. }
  34. k -= f;
  35. if (i == fr.begin())
  36. break;
  37. }
  38. if (!found) {
  39. cout << -1 << endl;
  40. }
  41. } else {
  42. int x, k; cin >> x >> k;
  43. auto it = fr.lower_bound(x);
  44. bool found = false;
  45. for (auto i = it; i != fr.end(); i++) {
  46. auto [e, f] = *i;
  47. if (f >= k) {
  48. found = true;
  49. cout << e << endl;
  50. break;
  51. }
  52. k -= f;
  53. }
  54. if (!found) {
  55. cout << -1 << endl;
  56. }
  57. }
  58. }
  59. // for (auto [e, f] : fr) {
  60. // cout << e << ' ' << f << endl;
  61. // }
  62. }
  63.  
  64.  
  65. signed main() {
  66. ios_base::sync_with_stdio(false);
  67. cin.tie(NULL); cout.tie(NULL);
  68. // #ifndef ONLINE_JUDGE
  69. // freopen("input.txt", "r", stdin);
  70. // freopen("output.txt", "w", stdout);
  71. // #endif
  72. int t; t = 1;
  73. // cin >> t;
  74. while (t--) solve();
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty