#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#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() {
cin.tie(0) -> sync_with_stdio(0);
int n; cin >> n;
int timer = 0;
vector<ll> inv(2 * n + 1);
map<int, Tree<int>> sets;
const auto dfs = [&](int idx, auto &&self) -> void {
int x; cin >> x;
if (x == 0) {
int idx_l = ++timer;
self(idx_l, self);
int idx_r = ++timer;
self(idx_r, self);
if (sz(sets[idx_l]) > sz(sets[idx_r])) {
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 += sz(sets[idx_r]) - loc;
}
for (int v : sets[idx_l]) {
sets[idx_r].insert(v);
}
inv[idx] = inv[idx_l] + inv[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(x);
}
};
dfs(0, dfs);
cout << inv[0] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
520 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1116 KB |
Output is correct |
2 |
Correct |
5 ms |
728 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
3188 KB |
Output is correct |
2 |
Correct |
23 ms |
1464 KB |
Output is correct |
3 |
Correct |
52 ms |
3156 KB |
Output is correct |
4 |
Correct |
13 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
4936 KB |
Output is correct |
2 |
Correct |
57 ms |
7760 KB |
Output is correct |
3 |
Correct |
68 ms |
10324 KB |
Output is correct |
4 |
Correct |
61 ms |
10324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
20440 KB |
Output is correct |
2 |
Correct |
97 ms |
15108 KB |
Output is correct |
3 |
Correct |
123 ms |
9556 KB |
Output is correct |
4 |
Correct |
80 ms |
8300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
10016 KB |
Output is correct |
2 |
Correct |
151 ms |
11860 KB |
Output is correct |
3 |
Correct |
124 ms |
21328 KB |
Output is correct |
4 |
Correct |
126 ms |
20808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
21564 KB |
Output is correct |
2 |
Correct |
224 ms |
15956 KB |
Output is correct |
3 |
Correct |
206 ms |
18572 KB |
Output is correct |
4 |
Correct |
221 ms |
17488 KB |
Output is correct |
5 |
Correct |
330 ms |
15700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
18704 KB |
Output is correct |
2 |
Correct |
245 ms |
32084 KB |
Output is correct |
3 |
Correct |
288 ms |
23244 KB |
Output is correct |
4 |
Correct |
214 ms |
33616 KB |
Output is correct |
5 |
Correct |
413 ms |
18636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
273 ms |
22100 KB |
Output is correct |
2 |
Correct |
258 ms |
18516 KB |
Output is correct |
3 |
Correct |
323 ms |
15760 KB |
Output is correct |
4 |
Correct |
275 ms |
21844 KB |
Output is correct |
5 |
Correct |
271 ms |
30384 KB |
Output is correct |
6 |
Correct |
344 ms |
19284 KB |
Output is correct |