-
Notifications
You must be signed in to change notification settings - Fork 1
/
1A.cpp
67 lines (52 loc) · 889 Bytes
/
1A.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
#include<stdio.h>
#include<iostream>
using namespace std;
typedef long long int ll;
ll countSplit(int a[], int l, int r)
{
int *c = new int[r-l+1];
int mid = (l+r)/2;
int n = mid+1, m = r+1;
int i = l, j = mid+1, k = 0;
ll ct = 0;
while(i<n && j<m)
{
if(a[i] <= a[j])
{
c[k++] = a[i++];
}
else
{
c[k++] = a[j++];
ct += (ll)(n-i);
}
}
while(i<n)
c[k++] = a[i++];
while(j<m)
c[k++] = a[j++];
for(int i=0;i<k;i++)
a[i+l] = c[i];
return ct;
}
ll countInv(int a[], int l, int r)
{
if(l == r)
return 0;
ll invLeft = countInv(a, l, (l+r)/2);
ll invRight = countInv(a, (l+r)/2 + 1, r);
ll invSplit = countSplit(a, l, r);
return invLeft+invRight+invSplit;
}
int main()
{
int i=0, n=100000;
freopen("IntegerArray.txt", "r", stdin);
int a[100001]={0};
while(i<n)
{
cin>>a[i++];
}
cout<<countInv(a, 0, n-1)<<endl;
return 0;
}