#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const int nx=4e5+5;
int n, bit[nx], root, cur, sz[nx];
vector<int> adj[nx], a[nx];
bool big[nx];
ll ans=0;
void upd(int x, int val)
{
for(; x <= n; x+=x&-x)
bit[x]+=val;
}
int get(int x)
{
int res=0;
for(; x > 0; x-=x&-x)
res+=bit[x];
return res;
}
int go()
{
int u;
cin>>u;
if(!u)
{
int l=go();
int r=go();
cur++;
adj[cur].emplace_back(l);
adj[cur].emplace_back(r);
return cur;
}
else return u;
}
int dfs1(int u)
{
sz[u]=1;
for(int v:adj[u])
sz[u]+=dfs1(v);
return sz[u];
}
void add(int u, int x)
{
upd(u, x);
for(int v:adj[u])
if(!big[v])
add(v, x);
}
void dfs(int u, bool keep)
{
if(u<=n)
a[u].emplace_back(u);
int mx=-1, bc=-1, sc=-1;
for(int v:adj[u])
if(sz[v]>mx)
mx=sz[v], bc=v;
for(int v:adj[u])
if(v!=bc)
dfs(v, 0), sc=v;
if(bc!=-1)
dfs(bc, 1), big[bc]=1;
if(sc!=-1)
{
ll l=0, r=0;
for(int c:a[sc])
l+=get(c-1), r+=get(n)-get(c);
ans+=min(l, r);
}
if(bc!=-1 && sc!=-1)
{
swap(a[u], a[bc]);
for(int c:a[sc])
a[u].emplace_back(c);
a[sc].clear();
}
add(u, 1);
if(bc!=-1)
big[bc]=0;
if(!keep)
add(u, -1);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
cur=n;
root=go();
dfs1(root);
dfs(root, 0);
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
21024 KB |
Output is correct |
3 |
Correct |
4 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21592 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
5 ms |
21592 KB |
Output is correct |
4 |
Correct |
5 ms |
21592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22876 KB |
Output is correct |
2 |
Correct |
11 ms |
22108 KB |
Output is correct |
3 |
Correct |
28 ms |
24260 KB |
Output is correct |
4 |
Correct |
10 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
25800 KB |
Output is correct |
2 |
Correct |
31 ms |
27228 KB |
Output is correct |
3 |
Correct |
36 ms |
29460 KB |
Output is correct |
4 |
Correct |
35 ms |
29528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
35636 KB |
Output is correct |
2 |
Correct |
46 ms |
33108 KB |
Output is correct |
3 |
Correct |
57 ms |
30932 KB |
Output is correct |
4 |
Correct |
44 ms |
29464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
30816 KB |
Output is correct |
2 |
Correct |
79 ms |
33364 KB |
Output is correct |
3 |
Correct |
64 ms |
37716 KB |
Output is correct |
4 |
Correct |
64 ms |
37456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
41948 KB |
Output is correct |
2 |
Correct |
129 ms |
39632 KB |
Output is correct |
3 |
Correct |
106 ms |
39100 KB |
Output is correct |
4 |
Correct |
120 ms |
39112 KB |
Output is correct |
5 |
Correct |
206 ms |
40060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
37912 KB |
Output is correct |
2 |
Correct |
126 ms |
47680 KB |
Output is correct |
3 |
Correct |
143 ms |
45004 KB |
Output is correct |
4 |
Correct |
109 ms |
48460 KB |
Output is correct |
5 |
Correct |
243 ms |
43220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
38504 KB |
Output is correct |
2 |
Correct |
123 ms |
40848 KB |
Output is correct |
3 |
Correct |
190 ms |
41032 KB |
Output is correct |
4 |
Correct |
146 ms |
40216 KB |
Output is correct |
5 |
Correct |
139 ms |
48076 KB |
Output is correct |
6 |
Correct |
219 ms |
43736 KB |
Output is correct |