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