#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
#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_multiset;
ordered_multiset nums[int(2e5)];
int main(){
cin.tie(NULL),cin.sync_with_stdio(false);
int n; cin >> n;
int m = n*2-2;
int cc = 0;
vvi adj(m+1);
auto build = [&](int& at, auto&& build) -> int {
int v; cin >> v;
if (v==0){
int my = at;
adj[my].push_back(build(--at,build));
adj[my].push_back(build(--at,build));
return my;
}else{
at++;
return v-1;
}
};
build(m,build);
auto dfs = [&](int at, auto&& dfs) -> array<ll,2> {
if (at<n){
nums[cc].insert(at);
return {0,cc++};
}else{
array<ll,2> inds;
ll ans = 0;
rep(i,0,2){
auto ret = dfs(adj[at][i],dfs);
ans += ret[0];
inds[i] = ret[1];
}
ll minext = 1e18;
if (sz(nums[inds[0]]) < sz(nums[inds[1]])) swap(inds[0],inds[1]);
{
//
ll extl = 0, extr = 0;
for(auto c : nums[inds[1]]){
int smaller = nums[inds[0]].order_of_key(c);
extl += sz(nums[inds[0]]) - smaller;
extr += smaller;
}
minext = min(extl, extr);
}
// merge small into large
for(auto c : nums[inds[1]]) nums[inds[0]].insert(c);
nums[inds[1]].clear();
return {ans + minext, inds[0]};
}
};
cout << dfs(n*2-2,dfs)[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15964 KB |
Output is correct |
2 |
Correct |
11 ms |
15960 KB |
Output is correct |
3 |
Correct |
12 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
5 |
Correct |
10 ms |
15992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15964 KB |
Output is correct |
2 |
Correct |
10 ms |
15960 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
16132 KB |
Output is correct |
2 |
Correct |
11 ms |
16220 KB |
Output is correct |
3 |
Correct |
11 ms |
16220 KB |
Output is correct |
4 |
Correct |
11 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16984 KB |
Output is correct |
2 |
Correct |
20 ms |
16476 KB |
Output is correct |
3 |
Correct |
13 ms |
16988 KB |
Output is correct |
4 |
Correct |
13 ms |
17020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
18776 KB |
Output is correct |
2 |
Correct |
23 ms |
17756 KB |
Output is correct |
3 |
Correct |
54 ms |
20560 KB |
Output is correct |
4 |
Correct |
20 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
23680 KB |
Output is correct |
2 |
Correct |
51 ms |
25680 KB |
Output is correct |
3 |
Correct |
63 ms |
28068 KB |
Output is correct |
4 |
Correct |
56 ms |
28576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
36664 KB |
Output is correct |
2 |
Correct |
74 ms |
34640 KB |
Output is correct |
3 |
Correct |
95 ms |
29776 KB |
Output is correct |
4 |
Correct |
77 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
32592 KB |
Output is correct |
2 |
Correct |
113 ms |
34192 KB |
Output is correct |
3 |
Correct |
103 ms |
42068 KB |
Output is correct |
4 |
Correct |
106 ms |
41812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
46676 KB |
Output is correct |
2 |
Correct |
184 ms |
41812 KB |
Output is correct |
3 |
Correct |
180 ms |
43604 KB |
Output is correct |
4 |
Correct |
186 ms |
42876 KB |
Output is correct |
5 |
Correct |
306 ms |
42068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
44416 KB |
Output is correct |
2 |
Correct |
182 ms |
56912 KB |
Output is correct |
3 |
Correct |
218 ms |
48988 KB |
Output is correct |
4 |
Correct |
152 ms |
58196 KB |
Output is correct |
5 |
Correct |
377 ms |
46316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
48972 KB |
Output is correct |
2 |
Correct |
211 ms |
45908 KB |
Output is correct |
3 |
Correct |
302 ms |
43784 KB |
Output is correct |
4 |
Correct |
242 ms |
48980 KB |
Output is correct |
5 |
Correct |
180 ms |
55120 KB |
Output is correct |
6 |
Correct |
310 ms |
47444 KB |
Output is correct |