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) {
- int j;
- bool x = int(floor(log2(i + 1)))%2;
- while (2*i + 1 < a.size()) {
- j = 2*i + 1;
- if (j + 1 < a.size() && (a[j + 1] < a[j])^x)
- ++j;
- for (int k = i*4 + 3; k < min(int(a.size()), i*4 + 7); ++k)
- if ((a[k] < a[j])^x)
- j = k;
- if (j >= 4*i + 3) {
- if ((a[j] < a[i])^x)
- swap(a[i], a[j]);
- if ((a[j] > a[(j - 1) >> 1])^x)
- swap(a[j], a[(j - 1) >> 1]);
- i = j;
- }
- else {
- if ((a[j] < a[i])^x)
- swap(a[i], a[j]);
- break;
- }
- }
- }
- void sift_up(int i, vector <int> &a) {
- while (i > 2) {
- if (int(floor(log2(i + 1)))%2 == 0)
- if (a[i] > a[(i - 1) >> 1])
- swap(a[i], a[(i - 1) >> 1]),
- i = (i - 1) >> 1;
- else
- if (i > 2 && a[i] < a[(((i - 1) >> 1) - 1) >> 1])
- swap(a[i], a[(((i - 1) >> 1) - 1) >> 1]),
- i = (((i - 1) >> 1) - 1) >> 1;
- else
- break;
- else
- if (a[i] < a[(i - 1) >> 1])
- swap(a[i], a[(i - 1) >> 1]),
- i = (i - 1) >> 1;
- else
- if (i > 2 && a[i] > a[(((i - 1) >> 1) - 1) >> 1])
- swap(a[i], a[(((i - 1) >> 1) - 1) >> 1]),
- i = (((i - 1) >> 1) - 1) >> 1;
- else
- break;
- }
- }
- int remove(int i, vector <int> &a) {
- int t = a[i];
- swap(a[i], a.back());
- a.erase(a.end() - 1);
- sift_down(i, a);
- return t;
- }
- void insert(int v, vector <int> &a) {
- a.push_back(v);
- sift_up(a.size() - 1, a);
- }
- int main() {
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- vector <int> a;
- string op;
- while (cin >> op) {
- if (op == "ins") {
- int x;
- cin >> x;
- insert(x, a);
- }
- if (op == "max")
- if (a.size() > 2)
- if (a[1] > a[2])
- cout << remove(1, a) << endl;
- else
- cout << remove(2, a) << endl;
- else
- if (a.size() == 1 || a[0] > a[1])
- cout << remove(0, a) << endl;
- else
- cout << remove(1, a) << endl;
- if (op == "min")
- cout << remove(0, a) << endl;
- cout << "##### ";
- for (int i : a)
- cout << i << ' ';
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement