#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5;
vector <int> g[N+9];
int n,value[N+9],bit[N+9],ans[N+9],x,i,l,r,siz[N+9];
void input(){
cin>>x;
if(x){
value[i]=x;
return;
}
int k=i;
i++;
g[k].push_back(i);
input();
i++;
g[k].push_back(i);
input();
}
void cnt(int i){
if(value[i]){
siz[i]=1;
return;
}cnt(g[i][0]);
cnt(g[i][1]);
siz[i]=siz[g[i][0]]+siz[g[i][1]];
}
void update(int i, int val){
while(i<=N){
bit[i]+=val;
i+=i&-i;
}
}
int get(int i){
int val=0;
while(i>0){
val+=bit[i];
i-=i&-i;
}
return val;
}
void add(int i, int val){
if (value[i]){
update(value[i],val);
return;
}
add(g[i][0],val);
add(g[i][1],val);
}
void tinh(int i){
if (value[i]){
l+=get(value[i]);
r+=get(n)-get(value[i]);
return;
}
for (int x : g[i]) tinh(x);
}
void solve(int i){
if (value[i]){
update(value[i],1);
return;
}
int x=g[i][0];
int y=g[i][1];
solve(x); ans[i]+=ans[x];
solve(y); ans[i]+=ans[y];
if (siz[x]>siz[y]) swap(x,y);
add(x,-1);
l=0;r=0;
tinh(x);
add(x,1);
ans[i]+=min(l,r);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
input();
cnt[0];
solve(0);
cout<<ans[0];
return 0;
}
Compilation message
rot.cpp: In function 'int32_t main()':
rot.cpp:80:10: warning: pointer to a function used in arithmetic [-Wpointer-arith]
80 | cnt[0];
| ^
rot.cpp:80:10: warning: value computed is not used [-Wunused-value]
80 | cnt[0];
| ~~~~~^
rot.cpp:80:10: warning: statement has no effect [-Wunused-value]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
18008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
18008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
247 ms |
18264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1033 ms |
18776 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
117 ms |
19548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1006 ms |
26472 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1102 ms |
23900 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1063 ms |
27220 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1029 ms |
26708 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1010 ms |
26964 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |