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, int &n, vector <pair <int, int>> &a, int *A, bool x) {
- int j;
- while (2*i + 1 < n) {
- j = 2*i + 1;
- if (j + 1 < n && (a[j + 1] < a[j])^x)
- ++j;
- if (a[i] == a[j] || (a[i] < a[j])^x)
- break;
- swap(a[i], a[j]);
- swap(A[i], A[j]);
- i = j;
- }
- }
- void sift_up(int i, vector <pair <int, int>> &a, int *A, bool x) {
- while (i && (a[i] < a[(i - 1) >> 1])^x)
- swap(a[i], a[(i - 1) >> 1]),
- swap(A[i], A[(i - 1) >> 1]),
- i = (i - 1) >> 1;
- }
- void remove(int &n, vector <pair <int, int>> &a, vector <pair <int, int>> &b, int *A, int *B, bool x) {
- swap(b[--n], b[B[a[0].S]]);
- swap(B[b[n].S], B[a[0].S]);
- swap(a[0], a[n]);
- sift_down(0, n, a, A, x);
- }
- void insert(int v, int n, vector <pair <int, int>> &a, int *A, bool x) {
- a[n] = {v, n};
- A[n] = n;
- sift_up(n++, a, A, x);
- }
- int main() {
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n = 0, x;
- vector <pair <int, int>> a(100), b(100);
- int A[100], B[100];
- while (cin >> x) {
- insert(x, n, a, A, 0);
- insert(x, n, b, B, 1);
- ++n;
- }
- for (int i = 0; i < n; ++i)
- cout << a[i].F << ' ' << b[i].F << endl;
- cout << endl;
- remove(n, a, b, A, B, 0);
- for (int i = 0; i < n; ++i)
- cout << a[i].F << ' ' << b[i].F << endl;
- cout << endl;
- remove(n, b, a, B, A, 1);
- for (int i = 0; i < n; ++i)
- cout << a[i].F << ' ' << b[i].F << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement