#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[2*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 |
18 ms |
31580 KB |
Output is correct |
2 |
Correct |
18 ms |
31576 KB |
Output is correct |
3 |
Correct |
19 ms |
31576 KB |
Output is correct |
4 |
Correct |
25 ms |
31580 KB |
Output is correct |
5 |
Correct |
24 ms |
31592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
31740 KB |
Output is correct |
2 |
Correct |
18 ms |
31580 KB |
Output is correct |
3 |
Correct |
19 ms |
31768 KB |
Output is correct |
4 |
Correct |
19 ms |
31580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
31836 KB |
Output is correct |
2 |
Correct |
19 ms |
31836 KB |
Output is correct |
3 |
Correct |
20 ms |
31836 KB |
Output is correct |
4 |
Correct |
20 ms |
31832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
32604 KB |
Output is correct |
2 |
Correct |
22 ms |
32604 KB |
Output is correct |
3 |
Correct |
21 ms |
32604 KB |
Output is correct |
4 |
Correct |
21 ms |
32856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
34068 KB |
Output is correct |
2 |
Correct |
34 ms |
34908 KB |
Output is correct |
3 |
Correct |
67 ms |
42832 KB |
Output is correct |
4 |
Correct |
28 ms |
34892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
47592 KB |
Output is correct |
2 |
Correct |
62 ms |
43856 KB |
Output is correct |
3 |
Correct |
68 ms |
45392 KB |
Output is correct |
4 |
Correct |
64 ms |
45648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
50000 KB |
Output is correct |
2 |
Correct |
82 ms |
49976 KB |
Output is correct |
3 |
Correct |
109 ms |
51864 KB |
Output is correct |
4 |
Correct |
82 ms |
46416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
50776 KB |
Output is correct |
2 |
Correct |
116 ms |
51280 KB |
Output is correct |
3 |
Correct |
106 ms |
56092 KB |
Output is correct |
4 |
Correct |
109 ms |
56156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
131 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
65536 KB |
Output is correct |
2 |
Runtime error |
128 ms |
65536 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
183 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |