#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) {
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 way_1 = 0; // way 1: left set is joined to the right set
ll way_2 = 0; // way 2: right set is joined to the left set
for (int v : sets[idx_l]) {
int loc = sets[idx_r].order_of_key(v);
way_1 += loc;
way_2 += (int)sets[idx_r].size() - loc;
}
for (int v : sets[idx_l]) { sets[idx_r].insert(v); }
sets[idx].swap(sets[idx_r]);
sets.erase(idx_l);
sets.erase(idx_r);
inversions[idx] = inversions[idx_l] + inversions[idx_r] + min(way_1, way_2);
} else if (val > 0) {
sets[idx].insert(val);
}
};
dfs(0);
cout << inversions[0] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
2 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1104 KB |
Output is correct |
2 |
Correct |
7 ms |
592 KB |
Output is correct |
3 |
Correct |
5 ms |
1360 KB |
Output is correct |
4 |
Correct |
6 ms |
1360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3408 KB |
Output is correct |
2 |
Correct |
21 ms |
1320 KB |
Output is correct |
3 |
Correct |
65 ms |
2888 KB |
Output is correct |
4 |
Correct |
20 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
4392 KB |
Output is correct |
2 |
Correct |
65 ms |
8060 KB |
Output is correct |
3 |
Correct |
82 ms |
10824 KB |
Output is correct |
4 |
Correct |
75 ms |
10824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
22228 KB |
Output is correct |
2 |
Correct |
117 ms |
15688 KB |
Output is correct |
3 |
Correct |
137 ms |
9596 KB |
Output is correct |
4 |
Correct |
120 ms |
8008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
9100 KB |
Output is correct |
2 |
Correct |
171 ms |
11592 KB |
Output is correct |
3 |
Correct |
162 ms |
22096 KB |
Output is correct |
4 |
Correct |
163 ms |
21576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
22784 KB |
Output is correct |
2 |
Correct |
256 ms |
16712 KB |
Output is correct |
3 |
Correct |
255 ms |
19684 KB |
Output is correct |
4 |
Correct |
302 ms |
17964 KB |
Output is correct |
5 |
Correct |
427 ms |
15788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
19616 KB |
Output is correct |
2 |
Correct |
327 ms |
34896 KB |
Output is correct |
3 |
Correct |
374 ms |
25240 KB |
Output is correct |
4 |
Correct |
298 ms |
36940 KB |
Output is correct |
5 |
Correct |
490 ms |
18504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
22344 KB |
Output is correct |
2 |
Correct |
351 ms |
19240 KB |
Output is correct |
3 |
Correct |
407 ms |
15804 KB |
Output is correct |
4 |
Correct |
378 ms |
22088 KB |
Output is correct |
5 |
Correct |
368 ms |
33352 KB |
Output is correct |
6 |
Correct |
406 ms |
19272 KB |
Output is correct |