-
Notifications
You must be signed in to change notification settings - Fork 1
/
INVER.cpp
executable file
·84 lines (60 loc) · 1.41 KB
/
INVER.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
83
84
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#define FOR(i,a,b) for(long long i = a ; i < b ; ++i )
#define PB push_back
typedef unsigned long long int uint64_t;
using namespace std;
void mergeSort( vector<uint64_t> & tab , int start, int stop, uint64_t * inv ){
if( start >= stop ) return;
vector <uint64_t> temp;
int partition = (start+stop)/2;
mergeSort( tab, start , partition , inv );
mergeSort( tab, partition+1 , stop , inv );
int it1 = start;
int it2 = partition+1;
while( it1 <= partition and it2 <= stop){
if( tab[ it2 ] < tab[ it1 ] ){
temp.PB( tab[ it2 ] );
(*inv) += ( partition - it1 + 1);
it2++;
}
else{
temp.PB( tab[ it1 ] );
it1++;
}
}
while( it1 <= partition ){ temp.PB( tab[ it1 ] ); it1++; }
while( it2 <= stop ){ temp.PB( tab[ it2 ] ); it2++; }
for( int i = 0 ; i < temp.size() ; ++i )
tab[ start + i ] = temp[ i ];
}
int main(){
uint64_t n;
uint64_t t;
uint64_t a;
uint64_t b;
uint64_t start;
uint64_t rodzaj;
uint64_t *result = new uint64_t;
vector < uint64_t > tab;
ios_base::sync_with_stdio( false );
cin >> t;
FOR( i , 0 , t ){
cin >> n ;
tab = vector< uint64_t >();
*result = (uint64_t)0;
FOR( i , 0 , n ){
cin >> a;
tab.PB( a );
}
mergeSort( tab, 0 , tab.size()-1 , result );
cout << *result << endl;
tab.clear();
}
return 0 ;
}