-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvcnt1.cpp
82 lines (80 loc) · 986 Bytes
/
invcnt1.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
80
81
82
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
long long int sum;
vector<int> mergesort(vector<int> v)
{
int i,j,n=v.size();
vector<int> a,b,r;
for(i=0;i<n;i++)
{
if(i>n/2)
b.push_back(v[i]);
else
a.push_back(v[i]);
}
a = mergesort(a.begin());
b = mergesort(b.begin());
i=j=0;
while(i<a.size())
{
while(j<b.size())
{
if(a[i]<b[j])
j++;
else
break;
}
sum+=j;
}
i=j=0;
while(i<a.size() && j<b.size())
{
if(a[i]<=b[j])
{
r.push_back(a[i]);
i++;
}
else
{
r.push_back(b[j]);
j++;
}
}
if(i==a.size())
while(j<b.size())
{
r.push_back(b[j]);
j++;
}
else
while(i<a.size())
{
r.push_back(a[i]);
i++;
}
return r;
}
int main()
{
int g;
scanf("%d",&g);
while(g)
{
int n,i;
scanf("%d",&n);
vector<int> a;
for(i=0;i<n;i++)
{
int y;
scanf("%d",&y);
a.push_back(y);
}
sum = 0;
mergesort(a.begin());
printf("%lld\n",sum);
g--;
}
return 0;
}