// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,fma,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define int long long
using namespace std;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=4e5;
vector <int> v,g[N+5];
int n,x,idx=0,cnt[N+5],sz[N+5],sum[N+5],FT[N+5];
void input(){
int x;
cin>>x;
cnt[idx]=x;
if (x) return;
int nw=idx;
g[nw].push_back(++idx);
input();
g[nw].push_back(++idx);
input();
}
void dfs(int i){
if (cnt[i]){
sz[i]=1;
return;
}
for (int x : g[i]){
dfs(x);
sz[i]+=sz[x];
}
}
void Update(int pos, int val){
for (;pos<=N;pos+=pos&(-pos)) FT[pos]+=val;
}
int Get(int pos){
int val=0;
for (;pos;pos-=pos&(-pos)) val+=FT[pos];
return val;
}
void cl(int i, int val){
if (cnt[i]){
Update(cnt[i],val);
return;
}
for (int x : g[i]) cl(x,val);
}
int lh,nh;
void Cal(int i){
if (cnt[i]){
lh+=Get(cnt[i]);
nh+=Get(n)-Get(cnt[i]);
return;
}
for (int x : g[i]) Cal(x);
}
void dfs1(int i){
if (cnt[i]){
Update(cnt[i],1);
return;
}
int x=g[i][0];
int y=g[i][1];
if (sz[x]>sz[y]) swap(x,y);
dfs1(x);
sum[i]+=sum[x];
cl(x,-1);
dfs1(y);
sum[i]+=sum[y];
lh=nh=0;
Cal(x);
cl(x,1);
sum[i]+=min(lh,nh);
}
void Solve(){
cin>>n;
input();
dfs(0);
dfs1(0);
cout<<sum[0];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen ("FILE.INP","r",stdin);
// freopen ("FILE.OUT","w",stdout);
int t=1;
// cin>>t;
while (t--) Solve();
cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9772 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
10328 KB |
Output is correct |
2 |
Correct |
6 ms |
10256 KB |
Output is correct |
3 |
Correct |
6 ms |
10328 KB |
Output is correct |
4 |
Correct |
6 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11356 KB |
Output is correct |
2 |
Correct |
12 ms |
10908 KB |
Output is correct |
3 |
Correct |
29 ms |
12380 KB |
Output is correct |
4 |
Correct |
10 ms |
11256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
14096 KB |
Output is correct |
2 |
Correct |
29 ms |
14944 KB |
Output is correct |
3 |
Correct |
30 ms |
16220 KB |
Output is correct |
4 |
Correct |
32 ms |
16476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
21340 KB |
Output is correct |
2 |
Correct |
39 ms |
19728 KB |
Output is correct |
3 |
Correct |
49 ms |
18260 KB |
Output is correct |
4 |
Correct |
39 ms |
17624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
20056 KB |
Output is correct |
2 |
Correct |
53 ms |
21076 KB |
Output is correct |
3 |
Correct |
52 ms |
23632 KB |
Output is correct |
4 |
Correct |
51 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
26904 KB |
Output is correct |
2 |
Correct |
100 ms |
26100 KB |
Output is correct |
3 |
Correct |
83 ms |
25940 KB |
Output is correct |
4 |
Correct |
103 ms |
25428 KB |
Output is correct |
5 |
Correct |
167 ms |
24404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
26716 KB |
Output is correct |
2 |
Correct |
103 ms |
31572 KB |
Output is correct |
3 |
Correct |
108 ms |
30032 KB |
Output is correct |
4 |
Correct |
82 ms |
32244 KB |
Output is correct |
5 |
Correct |
219 ms |
26456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
27220 KB |
Output is correct |
2 |
Correct |
86 ms |
28496 KB |
Output is correct |
3 |
Correct |
153 ms |
27128 KB |
Output is correct |
4 |
Correct |
112 ms |
27216 KB |
Output is correct |
5 |
Correct |
84 ms |
33104 KB |
Output is correct |
6 |
Correct |
217 ms |
26960 KB |
Output is correct |