#include <iostream>
#include <vector>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int c = 1;
ordered_set s[400005];
long long int dfs(int x) {
int w;
cin >> w;
if (w != 0) {
s[x].insert(w);
return 0;
}
long long int res = 0, d = 0, h = 1;
{
int w = ++c;
d += dfs(w);
h *= s[w].size();
int flag = 0;
if (s[w].size() > s[x].size()) {
s[x].swap(s[w]);
flag = 1;
}
for (int v : s[w]) {
if (flag == 1) res += s[x].order_of_key(v);
else res += s[x].size() - s[x].order_of_key(v);
}
for (int v : s[w]) {
s[x].insert(v);
}
s[w].clear();
}
{
int w = ++c;
d += dfs(w);
h *= s[w].size();
int flag = 0;
if (s[w].size() > s[x].size()) {
s[x].swap(s[w]);
flag = 1;
}
for (int v : s[w]) {
if (flag == 1) res += s[x].order_of_key(v);
else res += s[x].size() - s[x].order_of_key(v);
}
for (int v : s[w]) {
s[x].insert(v);
}
s[w].clear();
}
return min(res, h - res) + d;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
cout << dfs(1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
31580 KB |
Output is correct |
2 |
Correct |
24 ms |
31584 KB |
Output is correct |
3 |
Correct |
21 ms |
31580 KB |
Output is correct |
4 |
Correct |
21 ms |
31580 KB |
Output is correct |
5 |
Correct |
23 ms |
31580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
31580 KB |
Output is correct |
2 |
Correct |
20 ms |
31580 KB |
Output is correct |
3 |
Correct |
20 ms |
31596 KB |
Output is correct |
4 |
Correct |
21 ms |
31576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
31568 KB |
Output is correct |
2 |
Correct |
23 ms |
31796 KB |
Output is correct |
3 |
Correct |
22 ms |
31580 KB |
Output is correct |
4 |
Correct |
22 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
32348 KB |
Output is correct |
2 |
Correct |
25 ms |
32004 KB |
Output is correct |
3 |
Correct |
24 ms |
32600 KB |
Output is correct |
4 |
Correct |
23 ms |
32604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
34392 KB |
Output is correct |
2 |
Correct |
34 ms |
32472 KB |
Output is correct |
3 |
Correct |
67 ms |
33692 KB |
Output is correct |
4 |
Correct |
31 ms |
33872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
34896 KB |
Output is correct |
2 |
Correct |
59 ms |
38740 KB |
Output is correct |
3 |
Correct |
67 ms |
41964 KB |
Output is correct |
4 |
Correct |
63 ms |
41672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
51664 KB |
Output is correct |
2 |
Correct |
79 ms |
46416 KB |
Output is correct |
3 |
Correct |
102 ms |
39928 KB |
Output is correct |
4 |
Correct |
76 ms |
37456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
37968 KB |
Output is correct |
2 |
Correct |
111 ms |
41552 KB |
Output is correct |
3 |
Correct |
100 ms |
52008 KB |
Output is correct |
4 |
Correct |
119 ms |
51812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
228 ms |
50864 KB |
Output is correct |
2 |
Correct |
162 ms |
44116 KB |
Output is correct |
3 |
Correct |
173 ms |
46796 KB |
Output is correct |
4 |
Correct |
183 ms |
44612 KB |
Output is correct |
5 |
Correct |
310 ms |
43140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
45168 KB |
Output is correct |
2 |
Correct |
193 ms |
62804 KB |
Output is correct |
3 |
Correct |
225 ms |
53072 KB |
Output is correct |
4 |
Correct |
212 ms |
65536 KB |
Output is correct |
5 |
Correct |
388 ms |
45140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
48208 KB |
Output is correct |
2 |
Correct |
199 ms |
46256 KB |
Output is correct |
3 |
Correct |
286 ms |
42320 KB |
Output is correct |
4 |
Correct |
244 ms |
48276 KB |
Output is correct |
5 |
Correct |
199 ms |
61268 KB |
Output is correct |
6 |
Correct |
303 ms |
45732 KB |
Output is correct |