Advertisement
MathQ_

Untitled

Oct 27th, 2022
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. vector<int> c1, c2;
  2.  
  3. void count_inv(int l, int r, ll &cnt) {
  4.     if (l + 1 == r) return;
  5.     int m = l + r >> 1;
  6.     count_inv(l, m, cnt);
  7.     count_inv(m, r, cnt);
  8.     int i = l, j = m;
  9.     for (int k = l; k < r; ++k) {
  10.         if (i == m) {
  11.             c2[k] = c1[j];
  12.             ++j;
  13.         } else if (j == r) {
  14.             c2[k] = c1[i];
  15.             cnt += j - m;
  16.             ++i;
  17.         } else if (c1[i] < c1[j]) {
  18.             c2[k] = c1[i];
  19.             cnt += j - m;
  20.             ++i;
  21.         } else {
  22.             c2[k] = c1[j];
  23.             ++j;
  24.         }
  25.     }
  26.     for (int k = l; k < r; ++k) {
  27.         c1[k] = c2[k];
  28.     }
  29. }
  30.  
  31. int main() {
  32.     int n;
  33.     cin >> n;
  34.     vector<int> a(n);
  35.     cin >> a;
  36.     c1 = c2 = a;
  37.     ll ans = 0;
  38.     count_inv(0, n, ans);
  39.     cout << ans << '\n';
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement