#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 20
const ll MOD = 998244353;
const ll MAXN = 2e5 + 100;
vector<vector<int>> graph;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> st[MAXN];
int now = 0;
ll ans = 0;
int dfs() {
int q; cin >> q;
if (q == 0) {
int x = dfs();
int y = dfs();
if (st[y].size() > st[x].size())
swap(x, y);
ll all = st[x].size() * 1LL * st[y].size();
ll nw = 0;
for (auto u : st[y])
nw += st[x].order_of_key(u);
for (auto u : st[y])
st[x].insert(u);
ans += min(nw, all - nw);
return x;
} else {
now++;
st[now].insert(q);
return now;
}
}
int main() {
fast
int leaves;
cin >> leaves;
dfs();
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15960 KB |
Output is correct |
2 |
Correct |
13 ms |
16116 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
5 |
Correct |
10 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15964 KB |
Output is correct |
2 |
Correct |
10 ms |
15964 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16232 KB |
Output is correct |
2 |
Correct |
13 ms |
16284 KB |
Output is correct |
3 |
Correct |
11 ms |
16216 KB |
Output is correct |
4 |
Correct |
10 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
16728 KB |
Output is correct |
2 |
Correct |
14 ms |
16988 KB |
Output is correct |
3 |
Correct |
12 ms |
16732 KB |
Output is correct |
4 |
Correct |
14 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
18264 KB |
Output is correct |
2 |
Correct |
24 ms |
19292 KB |
Output is correct |
3 |
Correct |
61 ms |
27028 KB |
Output is correct |
4 |
Correct |
21 ms |
19032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
31508 KB |
Output is correct |
2 |
Correct |
57 ms |
27732 KB |
Output is correct |
3 |
Correct |
66 ms |
29524 KB |
Output is correct |
4 |
Correct |
56 ms |
29528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
34052 KB |
Output is correct |
2 |
Correct |
73 ms |
33616 KB |
Output is correct |
3 |
Correct |
103 ms |
35736 KB |
Output is correct |
4 |
Correct |
72 ms |
30032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
34132 KB |
Output is correct |
2 |
Correct |
105 ms |
34660 KB |
Output is correct |
3 |
Correct |
98 ms |
39660 KB |
Output is correct |
4 |
Correct |
104 ms |
39540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
180 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
50772 KB |
Output is correct |
2 |
Correct |
193 ms |
61036 KB |
Output is correct |
3 |
Correct |
233 ms |
61780 KB |
Output is correct |
4 |
Correct |
177 ms |
56744 KB |
Output is correct |
5 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
57156 KB |
Output is correct |
2 |
Correct |
196 ms |
48896 KB |
Output is correct |
3 |
Runtime error |
239 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |