-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinversion_count.cpp
More file actions
85 lines (81 loc) · 1.92 KB
/
inversion_count.cpp
File metadata and controls
85 lines (81 loc) · 1.92 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define int long long
#define pb push_back
#define pii pair<int,int>
#define setbits(x) __builtin_popcountll(x)
#define zerbefone(x) __builtin_ctzll(x)
#define pqb priority_queue<int> // maxheap
#define pqs priority_queue<int,vector<int>,greater<int> > // minheap
#define mod 1000000007
#define inf 100000000000000000 //1e17
#define Pi acos(-1.0)
#define precise(x,y) fixed<<setprecision(y)<<x
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int merge(int a[], int temp[], int l, int mid, int r)
{
int icnt = 0;
int i = l, j = mid, k = l;
while (i <= (mid - 1) && j <= r)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
{
icnt += mid - i;
temp[k++] = a[j++];
}
}
while (i <= mid - 1)
temp[k++] = a[i++];
while (j <= r)
temp[k++] = a[j++];
for (int idx = l; idx <= r; idx++)
a[idx] = temp[idx];
return icnt;
}
int ms(int a[], int temp[], int l, int r)
{
if (l >= r)
return 0;
int inv = 0, mid = (l + r) / 2;
inv += ms(a, temp, l, mid);
inv += ms(a, temp, mid + 1, r);
inv += merge(a, temp, l, mid + 1, r);
return inv;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("myinput.txt", "r", stdin);
freopen("myoutput.txt", "w", stdout);
#endif
FIO;
int test_cases = 1;
// cin >> test_cases;
for (int tt = 1; tt <= test_cases; tt++)
{
int n, ans = -1;
cin >> n;
vector<int> a(n);
int two[n] = {0};
for (int i = 0; i < n; i++)
{
cin >> a[i];
int p2 = 0;
while (a[i] % 2 == 0)
{
a[i] /= 2;
p2++;
}
two[n - 1 - i] = p2;
}
int temp[n] = {0};
cout << ms(two, temp, 0, n - 1);
}
}