forked from srikanthmalipatel/Spoj-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCEPC206-6813438-src.cpp
More file actions
63 lines (52 loc) · 1.04 KB
/
DCEPC206-6813438-src.cpp
File metadata and controls
63 lines (52 loc) · 1.04 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
#include<stdio.h>
#include <iostream>
using namespace std;
#define max 100001
int n;
long long res = 0;
int temp[max];
int arr[max];
void merge(int left, int m, int right)
{
int k = 0;
int i = left, j = m + 1;
while (i <= m && j <= right)
if (arr[i] < arr[j])
{
temp[k++] = arr[i];
res += (long long)(right - j + 1) * arr[i++];
}
else
temp[k++] = arr[j++];
while (j <= right)
temp[k++] = arr[j++];
while (i <= m)
temp[k++] = arr[i++];
for (int i = 0; i < k; ++i)
arr[left + i] = temp[i];
}
void sort(int left, int right)
{
if (left < right)
{
int m = left + (right - left) / 2;
sort(left, m);
sort(m + 1, right);
merge(left, m, right);
}
}
int main()
{
int t;
scanf("%d", &t);
for(int w=0;w<t;w++)
{
scanf("%d", &n);
for(int i=0;i<n;i++)
scanf("%d", &arr[i]);
res=0;
sort(0, n - 1);
printf("%lld\n",res);
}
return 0;
}