-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10810.cpp
47 lines (42 loc) · 1.34 KB
/
10810.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
#include <algorithm>
#include <iostream>
int main(void)
{
int length;
long count;
int array[500000], tmp_array[500000];
while (1) {
std::cin >> length;
if (!length) break;
for (int i = 0; i < length; ++i) {
std::cin >> array[i];
}
count = 0;
for (int seg = 1; seg < length; seg <<= 1) {
for (int i = 0; i < length; i += (seg << 1)) {
int beg1 = i;
int end1 = beg1 + seg > length ? length : beg1 + seg;
int beg2 = end1 > length ? length : end1;
int end2 = beg2 + seg > length ? length : beg2 + seg;
int idx = beg1;
while (beg1 < end1 && beg2 < end2) {
if (array[beg1] <= array[beg2])
tmp_array[idx++] = array[beg1++];
else {
tmp_array[idx++] = array[beg2++];
count += end1 - beg1;
}
}
while (beg1 < end1) {
tmp_array[idx++] = array[beg1++];
}
while (beg2 < end2) {
tmp_array[idx++] = array[beg2++];
}
}
std::swap(array, tmp_array);
}
std::cout << count << std::endl;
}
return 0;
}