Advertisement
Apkawa

Untitled

Sep 19th, 2021
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned ll
  4. #define ld long double
  5. #define be(a) a.begin(), a.end()
  6. #define L(x, n) for (int x = 0; x < n; ++x)
  7. #define F first
  8. #define S second
  9. #pragma GCC optimize("Ofast")
  10. using namespace std;
  11.  
  12. void sift_down(int i, int &n, vector <pair <int, int>> &a, int *A, bool x) {
  13.     int j;
  14.     while (2*i + 1 < n) {
  15.         j = 2*i + 1;
  16.         if (j + 1 < n && (a[j + 1] < a[j])^x)
  17.             ++j;
  18.         if (a[i] == a[j] || (a[i] < a[j])^x)
  19.             break;
  20.         swap(a[i], a[j]);
  21.         swap(A[i], A[j]);
  22.         i = j;
  23.     }
  24. }
  25.  
  26. void sift_up(int i, vector <pair <int, int>> &a, int *A, bool x) {
  27.     while (i && (a[i] < a[(i - 1) >> 1])^x)
  28.         swap(a[i], a[(i - 1) >> 1]),
  29.         swap(A[i], A[(i - 1) >> 1]),
  30.         i = (i - 1) >> 1;
  31. }
  32.  
  33. void remove(int &n, vector <pair <int, int>> &a, vector <pair <int, int>> &b, int *A, int *B, bool x) {
  34.     swap(b[--n], b[B[a[0].S]]);
  35.     swap(B[b[n].S], B[a[0].S]);
  36.     swap(a[0], a[n]);
  37.     sift_down(0, n, a, A, x);
  38. }
  39.  
  40. void insert(int v, int n, vector <pair <int, int>> &a, int *A, bool x) {
  41.     a[n] = {v, n};
  42.     A[n] = n;
  43.     sift_up(n++, a, A, x);
  44. }
  45.  
  46. int main() {
  47.     freopen("input.txt", "r", stdin);
  48.     //freopen("output.txt", "w", stdout);
  49.     ios_base::sync_with_stdio(0);
  50.     cin.tie(0);
  51.  
  52.     int n = 0, x;
  53.     vector <pair <int, int>> a(100), b(100);
  54.     int A[100], B[100];
  55.     while (cin >> x) {
  56.         insert(x, n, a, A, 0);
  57.         insert(x, n, b, B, 1);
  58.         ++n;
  59.     }
  60.     for (int i = 0; i < n; ++i)
  61.         cout << a[i].F << ' ' << b[i].F << endl;
  62.     cout << endl;
  63.     remove(n, a, b, A, B, 0);
  64.     for (int i = 0; i < n; ++i)
  65.         cout << a[i].F << ' ' << b[i].F << endl;
  66.     cout << endl;
  67.     remove(n, b, a, B, A, 1);
  68.     for (int i = 0; i < n; ++i)
  69.         cout << a[i].F << ' ' << b[i].F << endl;
  70.  
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement