Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> tree[1 << 17];
- int M = 1, liczba_inwersji = 0;
- void f(int v){ //merge sort
- int lw = 0, pw = 0, n = tree[2 * v].size();
- while(lw < n || pw < n){
- if(lw < n && tree[v * 2][lw] <= tree[v * 2 + 1][pw])
- tree[v].push_back(tree[v * 2][lw++]);
- else{
- tree[v].push_back(tree[v * 2 + 1][pw++]);
- liczba_inwersji += n - lw;
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n;
- cin >> n;
- while(M < n) M *= 2;
- for(int i = M, a;i < n + M;i++){
- cin >> a;
- tree[i].push_back(a);
- }
- for(int i = n + M;i < 2 * M;i++)
- tree[i].push_back(INT_MAX);
- for(int i = M - 1;i > 0;i--)
- f(i);
- cout << liczba_inwersji;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement