#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(4e5)];
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);
return {ans + minext, inds[0]};
}
};
cout << dfs(n*2-2,dfs)[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
31580 KB |
Output is correct |
2 |
Correct |
23 ms |
31572 KB |
Output is correct |
3 |
Correct |
22 ms |
31576 KB |
Output is correct |
4 |
Correct |
21 ms |
31580 KB |
Output is correct |
5 |
Correct |
24 ms |
31580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
31580 KB |
Output is correct |
2 |
Correct |
22 ms |
31832 KB |
Output is correct |
3 |
Correct |
22 ms |
31580 KB |
Output is correct |
4 |
Correct |
23 ms |
31580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
31836 KB |
Output is correct |
2 |
Correct |
29 ms |
31836 KB |
Output is correct |
3 |
Correct |
23 ms |
31848 KB |
Output is correct |
4 |
Correct |
24 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
32852 KB |
Output is correct |
2 |
Correct |
25 ms |
32860 KB |
Output is correct |
3 |
Correct |
28 ms |
32848 KB |
Output is correct |
4 |
Correct |
26 ms |
32860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
34900 KB |
Output is correct |
2 |
Correct |
34 ms |
35792 KB |
Output is correct |
3 |
Correct |
68 ms |
45220 KB |
Output is correct |
4 |
Correct |
30 ms |
35924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
51540 KB |
Output is correct |
2 |
Correct |
64 ms |
47444 KB |
Output is correct |
3 |
Correct |
71 ms |
49736 KB |
Output is correct |
4 |
Correct |
66 ms |
50004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
56148 KB |
Output is correct |
2 |
Correct |
84 ms |
56404 KB |
Output is correct |
3 |
Correct |
114 ms |
58452 KB |
Output is correct |
4 |
Correct |
87 ms |
53276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
60244 KB |
Output is correct |
2 |
Correct |
129 ms |
60348 KB |
Output is correct |
3 |
Correct |
124 ms |
65464 KB |
Output is correct |
4 |
Correct |
123 ms |
65388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
110 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
120 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |