#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
int n;
cin >> n;
int timer = 0;
vector<ll> inversions(2 * n - 1);
map<int, Tree<int>> sets;
function<void(int)> dfs = [&](int idx) {
assert(idx < (int) inversions.size());
int val;
cin >> val;
if (val == 0) {
int idx_l = ++timer;
dfs(idx_l);
int idx_r = ++timer;
dfs(idx_r);
if (sets[idx_l].size() > sets[idx_r].size()) {
sets[idx_l].swap(sets[idx_r]);
}
ll opt_1 = 0, opt_2 = 0;
for (int v : sets[idx_l]) {
int loc = sets[idx_r].order_of_key(v);
opt_1 += loc;
opt_2 += (int) sets[idx_r].size() - loc;
}
for (int v : sets[idx_l]) {
sets[idx_r].insert(v);
}
inversions[idx] = inversions[idx_l] + inversions[idx_r]
+ min(opt_1, opt_2);
sets[idx].swap(sets[idx_r]);
sets.erase(idx_l);
sets.erase(idx_r);
} else {
sets[idx].insert(val);
}
};
dfs(0);
cout << inversions[0] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
4 ms |
1512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
3416 KB |
Output is correct |
2 |
Correct |
18 ms |
1372 KB |
Output is correct |
3 |
Correct |
56 ms |
2964 KB |
Output is correct |
4 |
Correct |
16 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
4892 KB |
Output is correct |
2 |
Correct |
69 ms |
8532 KB |
Output is correct |
3 |
Correct |
77 ms |
11348 KB |
Output is correct |
4 |
Correct |
71 ms |
11092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
22840 KB |
Output is correct |
2 |
Correct |
108 ms |
16308 KB |
Output is correct |
3 |
Correct |
133 ms |
10068 KB |
Output is correct |
4 |
Correct |
96 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
9808 KB |
Output is correct |
2 |
Correct |
172 ms |
12628 KB |
Output is correct |
3 |
Correct |
146 ms |
23096 KB |
Output is correct |
4 |
Correct |
149 ms |
22628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
22840 KB |
Output is correct |
2 |
Correct |
252 ms |
16724 KB |
Output is correct |
3 |
Correct |
238 ms |
19696 KB |
Output is correct |
4 |
Correct |
263 ms |
18048 KB |
Output is correct |
5 |
Correct |
354 ms |
15700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
19308 KB |
Output is correct |
2 |
Correct |
285 ms |
34896 KB |
Output is correct |
3 |
Correct |
338 ms |
25124 KB |
Output is correct |
4 |
Correct |
259 ms |
36868 KB |
Output is correct |
5 |
Correct |
436 ms |
18548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
316 ms |
22356 KB |
Output is correct |
2 |
Correct |
291 ms |
19276 KB |
Output is correct |
3 |
Correct |
349 ms |
15780 KB |
Output is correct |
4 |
Correct |
318 ms |
22092 KB |
Output is correct |
5 |
Correct |
327 ms |
33616 KB |
Output is correct |
6 |
Correct |
376 ms |
19284 KB |
Output is correct |