Advertisement
Galebickosikasa

Untitled

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