-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path10810.cpp
More file actions
56 lines (51 loc) · 1.1 KB
/
10810.cpp
File metadata and controls
56 lines (51 loc) · 1.1 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
#include <iostream>
#define ll unsigned long long
using namespace std;
int arr[500000], cparr[500000], n;
ll check() {
ll ans = 0;
for (int i = 0; i < n-1; i++)
for (int j = i+1; j < n; j++)
if (arr[i] > arr[j]) ans++;
return ans;
}
ll merge(int s, int e) {
int as = s;
int m = (s+e)/2;
int me = m+1;
int cp = 0;
ll ans = 0;
while(s <= m and me <= e) {
if (arr[s] <= arr[me]) cparr[cp++] = arr[s++];
else {
ans += (m+1) - s;
cparr[cp++] = arr[me++];
}
}
while(me <= e) cparr[cp++] = arr[me++];
while(s <= m) cparr[cp++] = arr[s++];
for (int i = 0; i < cp; i++) arr[as++] = cparr[i];
return ans;
}
ll ms(int s, int e) {
if (s >= e) return 0;
ll m = (s+e)/2;
ll a1 = ms(s, m), a2 = ms(m+1, e);
ll a3 = merge(s, e);
return a1 + a2 + a3;
}
int main() {
while (true) {
cin >> n;
if (n == 0) return 0;
for (int i = 0; i < n; i++) cin >> arr[i];
// int ans = check();
ll a = ms(0, n-1);
/*
if (ans != a) {
cout << "ANSWERS DONT MATCH, GOT " << a << " EXPECTED " << ans << endl;
}
*/
cout << a << endl;
}
}