-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINVCNT.cpp
More file actions
83 lines (81 loc) · 1.54 KB
/
Copy pathINVCNT.cpp
File metadata and controls
83 lines (81 loc) · 1.54 KB
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
80
81
82
83
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<cctype>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
using namespace std;
#define pb push_back
#define pi 3.141592653589793238462643383
long long merge(int a[],int l,int mid,int h,int temp[]){
int i,j,k;
long long count=0;
j = mid+1;
for(k=l;k<=mid;k++){
while(j<=h){
if(a[k]>a[j])
j++;
else
break;
}
count+=j-(mid+1);
}
i = l;
j = mid+1;
k = l;
while(i<=mid&&j<=h){
if(a[i]<a[j]){
temp[k] = a[i];
i++;k++;
}
else
{
temp[k] = a[j];
j++;
k++;
}
}
while(i<=mid){
temp[k] = a[i];
i++;k++;
}
while(j<=h){
temp[k] = a[j];
j++;k++;
}
for(k=l;k<=h;k++)
a[k] = temp[k];
return count;
}
long long mergesort(int a[],int l,int h,int temp[]){
if(l==h)
return 0;
int mid;
mid = (l+h)/2;
long long lc,rc,mc;
lc = mergesort(a,l,mid,temp);
rc = mergesort(a,mid+1,h,temp);
mc = merge(a,l,mid,h,temp);
return lc+rc+mc;
}
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t>0){
scanf("%d",&n);
int a[n],temp[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
long long ans = mergesort(a,0,n-1,temp);
printf("%lld\n",ans);
t--;
}
}