Advertisement
Galebickosikasa

Untitled

Dec 20th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <unordered_set>
  6. #include <unordered_map>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <deque>
  11. #include <bitset>
  12. #include <stack>
  13. #include <random>
  14. #include <fstream>
  15. #include <sstream>
  16. #include <chrono>
  17.  
  18. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  19. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  20.  
  21. using namespace std;
  22.  
  23. const int maxn = 2e5 + 20;
  24.  
  25. ostream& operator << (ostream& out, const pair<int, int>& v) {
  26. out << v.first << ", " << v.second;
  27. return out;
  28. }
  29.  
  30. template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
  31. for (auto& x: v) out << x << " ";
  32. return out;
  33. }
  34.  
  35. istream& operator >> (istream& in, pair<int, int>& a) {
  36. in >> a.first >> a.second;
  37. return in;
  38. }
  39.  
  40. const long long inf = (long long) 2e9;
  41. const long double pi = asin (1) * 2;
  42. const long double eps = 1e-8;
  43. const long long mod = (long long)1e9 + 7;
  44. const long long ns = 97;
  45.  
  46. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  47.  
  48. struct tree {
  49. vector<int> t;
  50. int size = 0, capacity;
  51.  
  52. tree (int n) {
  53. while ((1<<size) < n) ++size;
  54. ++size;
  55. size = (1<<size);
  56. capacity = (size>>1);
  57. t = vector<int> (size);
  58. }
  59.  
  60. int get (int cur, int k) {
  61. if (cur * 2 >= size) return cur - capacity;
  62. if (t[cur * 2] >= k) return get (cur * 2, k);
  63. return get (cur * 2 + 1, k - t[cur * 2]);
  64. }
  65.  
  66. void add (int i, int x) {
  67. i += capacity;
  68. t[i] += x;
  69. i >>= 1;
  70. while (i) {
  71. t[i] = t[i * 2] + t[i * 2 + 1];
  72. i >>= 1;
  73. }
  74. }
  75. };
  76.  
  77. signed main () {
  78. ios_base::sync_with_stdio(false);
  79. cin.tie(nullptr);
  80. cout.tie(nullptr);
  81. int n;
  82. cin >> n;
  83. vector<pair<int, int> > kek;
  84. for (int i = 0; i < n; ++i) {
  85. string t;
  86. int x = 0;
  87. cin >> t;
  88. if (t == "add") {
  89. cin >> x;
  90. kek.push_back ({0, x});
  91. }
  92. if (t == "min") kek.push_back ({1, x});
  93. if (t == "max") kek.push_back ({2, x});
  94. if (t == "mid") kek.push_back ({3, x});
  95. }
  96. vector<int> tmp;
  97. for (auto& x: kek) tmp.push_back (x.second);
  98. sort (tmp.begin(), tmp.end());
  99. tmp.resize (unique (tmp.begin(), tmp.end()) - tmp.begin ());
  100. for (auto& x: kek) x.second = (int)(lower_bound (tmp.begin(), tmp.end(), x.second) - tmp.begin ());
  101. int m = (int)tmp.size();
  102. tree goo (m);
  103. int cnt = 0;
  104. for (auto& t: kek) {
  105. if (t.first == 0) {
  106. goo.add (t.second, 1);
  107. ++cnt;
  108. } else if (t.first == 1) {
  109. int x = goo.get (1, 1);
  110. cout << tmp[x] << '\n';
  111. goo.add (x, -1);
  112. --cnt;
  113. } else if (t.first == 2) {
  114. int x = goo.get (1, cnt);
  115. cout << tmp[x] << '\n';
  116. goo.add (x, -1);
  117. --cnt;
  118. } else {
  119. int x = goo.get (1, (cnt + 1) / 2);
  120. cout << tmp[x] << '\n';
  121. goo.add (x, -1);
  122. --cnt;
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement