Advertisement
Korotkodul

инверсии OK

Sep 24th, 2022 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49. ll ans = 0;
  50. bool sh = 0;
  51. vector <int> a;
  52. void mrg(int l, int m, int r) {
  53.     if (sh) {
  54.         cout << "l r = " << l << ' ' << r << "\n";
  55.     }
  56.     int k = m;
  57.     for (int i = l; i <= m ; ++i) {
  58.         if (a[m + 1] >= a[i]) continue;
  59.         if (sh) {
  60.             cout << "i = " << i << "\n";
  61.             cout << "a\n"; cv(a);
  62.         }
  63.         int L = m + 1, R =  r + 1, M;
  64.         while (L + 1 < R) {
  65.             M = (L + R) / 2;
  66.             if (sh) {
  67.                 cout << "M = " << M << "\n";
  68.             }
  69.             if (a[M] >= a[i]) {
  70.                 R = M;
  71.             } else {
  72.                 L = M;
  73.             }
  74.         }
  75.         ans += (m - i + 1) * (L - k);
  76.         if (sh) {
  77.             cout << "ans += " << (m - i + 1) * (L - k) << "\n";
  78.         }
  79.         k = L;
  80.     }
  81.  
  82.     vector <int> res;
  83.     int A = l, B = m + 1;
  84.     while (A <= m && B <= r) {
  85.         if (a[A] < a[B]) {
  86.             res.pb(a[A]);
  87.             A++;
  88.         } else {
  89.             res.pb(a[B]);
  90.             B++;
  91.         }
  92.     }
  93.     while (A <= m) {
  94.         res.pb(a[A]);
  95.         A++;
  96.     }
  97.     while (B <= r) {
  98.         res.pb(a[B]);
  99.         B++;
  100.     }
  101.     for (int i = l; i <= r; ++i) {
  102.         a[i] = res[i - l];
  103.     }
  104. }
  105.  
  106. void f(int l, int r) {
  107.     if (l == r) {
  108.         return;
  109.     } else if (l + 1 == r) {
  110.         if (a[l] > a[r]) {
  111.             ans++;
  112.             swap(a[l], a[r]);
  113.         }
  114.         return;
  115.     }
  116.  
  117.     int m = (l + r) / 2;
  118.  
  119.     f(l, m);
  120.  
  121.     f(m + 1, r);
  122.     mrg(l, m, r);
  123. }
  124.  
  125. /*
  126. 5
  127. 179 4 3 2 1
  128.  
  129.  
  130. 9
  131. 179 4 3 2 1 11 100 0 2
  132. */
  133.  
  134. int main() {
  135.     /*ios::sync_with_stdio(0);
  136.     cin.tie(0);
  137.     cout.tie(0);*/
  138.     int n; cin >> n;
  139.     a.resize(n);
  140.     for (int &i : a) cin >> i;
  141.  
  142.     f(0, n - 1);
  143.     cout << ans;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement