// #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=2e5;
vector <int> v;
pair <int,int> mp[2*N+5];
int n,x,idx=0,cnt[2*N+5],sz[2*N+5],sum[2*N+5],FT[N+5];
void input(){
int x;
cin>>x;
cnt[idx]=x;
if (x) return;
int nw=idx;
mp[nw].first=++idx;
input();
mp[nw].second=++idx;
input();
}
void dfs(int i){
if (cnt[i]){
sz[i]=1;
return;
}
int x=mp[i].first;
int y=mp[i].second;
sz[i]=sz[x]+sz[y];
}
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;
}
int x=mp[i].first;
int y=mp[i].second;
cl(x,val);
cl(y,val);
}
int lh,nh;
void Cal(int i){
if (cnt[i]){
lh+=Get(cnt[i]);
nh+=Get(n)-Get(cnt[i]);
return;
}
int x=mp[i].first;
int y=mp[i].second;
Cal(x);
Cal(y);
}
void dfs1(int i){
if (cnt[i]){
Update(cnt[i],1);
return;
}
int x=mp[i].first;
int y=mp[i].second;
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
4804 KB |
Output is correct |
2 |
Correct |
4 ms |
4696 KB |
Output is correct |
3 |
Correct |
74 ms |
4700 KB |
Output is correct |
4 |
Correct |
111 ms |
4848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1073 ms |
5212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
11292 KB |
Output is correct |
2 |
Execution timed out |
1079 ms |
10076 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1039 ms |
15184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1016 ms |
13656 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1027 ms |
17440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1026 ms |
15184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1032 ms |
15196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |