Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned ll
- #define ld long double
- #define be(a) a.begin(), a.end()
- #define L(x, n) for (int x = 0; x < n; ++x)
- #define F first
- #define S second
- #pragma GCC optimize("Ofast")
- using namespace std;
- void sift_down(int i, vector <int> &a, bool x) {
- int j;
- while (2*i + 1 < a.size()) {
- j = 2*i + 1;
- if (j + 1 < a.size() && (a[j + 1] < a[j])^x)
- ++j;
- if (a[i] == a[j] || (a[i] < a[j])^x)
- break;
- swap(a[i], a[j]);
- i = j;
- }
- }
- void sift_up(int i, vector <int> &a, bool x) {
- while (i && (a[i] < a[(i - 1) >> 1])^x)
- swap(a[i], a[(i - 1) >> 1]),
- i = (i - 1) >> 1;
- }
- int remove(vector <int> &a, bool x) {
- int t = a[0];
- swap(a[0], a.back());
- a.erase(a.end() - 1);
- sift_down(0, a, x);
- return t;
- }
- void insert(int v, vector <int> &a, bool x) {
- a.push_back(v);
- sift_up(a.size() - 1, a, x);
- }
- int main() {
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- vector <int> a, b;
- string op;
- while (cin >> op) {
- if (op == "ins") {
- int x;
- cin >> x;
- if (a.size())
- if (x > a[0])
- insert(x, a, 0);
- else
- insert(x, b, 1);
- else
- insert(x, b, 1);
- if (a.size() > b.size())
- insert(remove(a, 0), b, 1);
- else
- if (a.size() + 1 < b.size())
- insert(remove(b, 1), a, 0);
- }
- if (op == "med")
- cout << b[0] << endl;
- if (op == "remove_med")
- cout << remove(b, 1) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement