#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define int long long
using namespace std;
const int N=8e5+5;
int bit[N],sz[N],value[N],id=0,n,ans[N],l,r;
vector<int>g[N];
void input(){
int x;
cin>>x;
value[id]=x;
if (x) return;
int k=id;
g[k].push_back(++id);
input();
g[k].push_back(++id);
input();
}
int getSum(int p) {
int idx = p, res = 0;
while (idx > 0) {
res += bit[idx];
idx -= (idx & (-idx));
}
return res;
}
void update(int u, int v) {
int idx = u;
while (idx <= N) {
bit[idx] += v;
idx += (idx & (-idx));
}
}
void cnt(int id){
if(value[id]){
sz[id]=1;
return;
}for(int x:g[id]){
cnt(x);
sz[id]+=sz[x];
}return;
}
void add(int id, int val){
if(value[id]){
update(value[id],val);
return;
}for(int x:g[id]) add(x,val);
}
void cal(int id){
if(value[id]){
l+=getSum(value[id]);
r+=getSum(n)-getSum(value[id]);
return;
}for(int x:g[id]) cal(x);
}
void solve(int id){
if(value[id]){
update(value[id],1);
return;
}int x=g[id][0];
int y=g[id][1];
if(sz[x]>sz[y]) swap(x,y);
solve(x);
ans[id]+=ans[x];
add(x,-1);
solve(y);
ans[id]+=ans[y];
l=0;r=0;
cal(x);
add(x,1);
ans[id]+=min(l,r);
return;
}
signed main() {
cin>>n;
input();
cnt(0);
solve(0);
cout<<ans[0];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19292 KB |
Output is correct |
2 |
Correct |
9 ms |
19260 KB |
Output is correct |
3 |
Correct |
9 ms |
19248 KB |
Output is correct |
4 |
Correct |
9 ms |
19288 KB |
Output is correct |
5 |
Correct |
8 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19292 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19080 KB |
Output is correct |
4 |
Correct |
9 ms |
19228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19240 KB |
Output is correct |
2 |
Correct |
9 ms |
19288 KB |
Output is correct |
3 |
Correct |
9 ms |
19144 KB |
Output is correct |
4 |
Correct |
13 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19804 KB |
Output is correct |
2 |
Correct |
13 ms |
19616 KB |
Output is correct |
3 |
Correct |
12 ms |
19804 KB |
Output is correct |
4 |
Correct |
14 ms |
19760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
20828 KB |
Output is correct |
2 |
Correct |
18 ms |
20312 KB |
Output is correct |
3 |
Correct |
51 ms |
22096 KB |
Output is correct |
4 |
Correct |
17 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
24036 KB |
Output is correct |
2 |
Correct |
48 ms |
24660 KB |
Output is correct |
3 |
Correct |
48 ms |
26196 KB |
Output is correct |
4 |
Correct |
71 ms |
26348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
31316 KB |
Output is correct |
2 |
Correct |
62 ms |
29524 KB |
Output is correct |
3 |
Correct |
80 ms |
28116 KB |
Output is correct |
4 |
Correct |
75 ms |
27692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
30140 KB |
Output is correct |
2 |
Correct |
105 ms |
31240 KB |
Output is correct |
3 |
Correct |
90 ms |
33644 KB |
Output is correct |
4 |
Correct |
86 ms |
33360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
37460 KB |
Output is correct |
2 |
Correct |
149 ms |
36628 KB |
Output is correct |
3 |
Correct |
132 ms |
36432 KB |
Output is correct |
4 |
Correct |
154 ms |
35924 KB |
Output is correct |
5 |
Correct |
233 ms |
34960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
37456 KB |
Output is correct |
2 |
Correct |
148 ms |
42064 KB |
Output is correct |
3 |
Correct |
169 ms |
40552 KB |
Output is correct |
4 |
Correct |
129 ms |
42852 KB |
Output is correct |
5 |
Correct |
280 ms |
37204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
37784 KB |
Output is correct |
2 |
Correct |
129 ms |
39252 KB |
Output is correct |
3 |
Correct |
207 ms |
37712 KB |
Output is correct |
4 |
Correct |
156 ms |
38064 KB |
Output is correct |
5 |
Correct |
121 ms |
43856 KB |
Output is correct |
6 |
Correct |
320 ms |
37708 KB |
Output is correct |