-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCODESPTB.cpp
More file actions
59 lines (58 loc) · 1.11 KB
/
CODESPTB.cpp
File metadata and controls
59 lines (58 loc) · 1.11 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
#include<stdio.h>
//#include<conio.h>
static long long int cntr=0;
void merge(long long int [],int,int,int);
void mergeSort(long long int arr[],int low, int high){
if(low>=high)return;
int mid=(low+high)/2;
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,high);
}
void merge(long long int arr[], int low, int mid, int high){
long long int a[high-low+1];
int i = low;
int j = mid+1;
int k = 0;
while(i<=mid && j<=high){
if(arr[i]>arr[j]){
a[k]=arr[j];
cntr+=mid-i+1;
j++;
}
else{
a[k]=arr[i];
i++;
}
k++;
}
while(i<=mid){
a[k]=arr[i];
k++;i++;
}
while(j<=high){
a[k]=arr[j];
k++;j++;
}
int p=0;
for(int m=low;m<=high;m++,p++){
arr[m]=a[p];
}
}
int main(){
int t;
scanf("%d",&t);
for(;t>0;t--){
cntr=0;
long long int n;
scanf("%lld",&n);
long long int arr[n];
for(int i=0;i<n;i++){
scanf("%lld",&arr[i]);
}
mergeSort(arr, 0, n-1);
printf("%lld\n",cntr);
}
//getch();
return 0;
}