Advertisement
Rentib

Untitled

Mar 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> tree[1 << 17];
  4. int M = 1, liczba_inwersji = 0;
  5. void f(int v){ //merge sort
  6. int lw = 0, pw = 0, n = tree[2 * v].size();
  7. while(lw < n || pw < n){
  8. if(lw < n && tree[v * 2][lw] <= tree[v * 2 + 1][pw])
  9. tree[v].push_back(tree[v * 2][lw++]);
  10. else{
  11. tree[v].push_back(tree[v * 2 + 1][pw++]);
  12. liczba_inwersji += n - lw;
  13. }
  14. }
  15. }
  16. int main(){
  17. ios_base::sync_with_stdio(0);
  18. cin.tie(0);
  19. int n;
  20. cin >> n;
  21. while(M < n) M *= 2;
  22. for(int i = M, a;i < n + M;i++){
  23. cin >> a;
  24. tree[i].push_back(a);
  25. }
  26. for(int i = n + M;i < 2 * M;i++)
  27. tree[i].push_back(INT_MAX);
  28. for(int i = M - 1;i > 0;i--)
  29. f(i);
  30. cout << liczba_inwersji;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement