-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inversion_Count.cpp
79 lines (69 loc) · 1.29 KB
/
Inversion_Count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll count;
void Merge(ll l, ll m, ll r, vector<ll> &arr)
{
ll l_size = m-l+1, r_size = r-m, i, j;
vector<ll> left, right;
for(i=l;i<=m;i+=1)
left.push_back(arr[i]);
for(i=m+1;i<=r;i+=1)
right.push_back(arr[i]);
i = 0;
j = 0;
while(i<l_size and j<r_size)
{
if(left[i]>right[j])
{
arr[l] = right[j];
::count+=(l_size-i);
j+=1;
}
else
{
arr[l] = left[i];
i+=1;
}
l+=1;
}
while(i<l_size)
{
arr[l] = left[i];
i+=1;
l+=1;
}
while(j<r_size)
{
arr[l] = right[j];
j+=1;
l+=1;
}
}
void MergeSort(ll l, ll r, vector<ll> &arr)
{
if(l==r)
return;
ll m = (l+r)/2;
MergeSort(l, m, arr);
MergeSort(m+1, r, arr);
Merge(l, m, r, arr);
}
void solve()
{
ll i, n;
::count = 0;
cin>>n;
vector<ll> arr(n);
for(i=0;i<n;i+=1)
cin>>arr[i];
MergeSort(0, n-1, arr);
cout<<::count<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
}