-
Notifications
You must be signed in to change notification settings - Fork 0
/
1301.cpp
58 lines (54 loc) · 1.15 KB
/
1301.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
#include <iostream>
#include <vector>
using std::cin, std::vector, std::cout;
int n;
vector<int> swap_time;
vector<int> data;
void compute_reverse(vector<int>::iterator l, vector<int>::iterator r) {
if (l == r) return;
if (l + 1 == r) return;
//if (r == l + 1) {
// if (*r < *l) {
// swap_time[*l]++;
// swap_time[*r]++;
// std::swap(l, r);
// }
// return;
//}
auto m = l + (r - l) / 2;
compute_reverse(l, m);
compute_reverse(m, r);
vector<int> temp;
temp.resize(r - l);
auto f = temp.begin();
auto lw = l, rw = m;
while (lw < m && rw < r) {
if (*rw < *lw) {
swap_time[*rw] += (m - lw);
*(f++) = *(rw++);
}
else {
swap_time[*lw] += (rw - m);
*(f++) = *(lw++);
}
}
while (lw < m) {
swap_time[*lw] += (rw - m);
*(f++) = *(lw++);
}
while (rw < r) {
*(f++) = *(rw++);
}
for (auto i : temp) {
*(l++) = i;
}
}
int main() {
cin >> n;
data.resize(n);
swap_time.resize(n + 1);
for (int i = 0; i < n; ++i) cin >> data[i];
compute_reverse(data.begin(), data.end());
for (auto i = swap_time.begin() + 1; i < swap_time.end(); ++i) cout << *i << ' ';
return 0;
}