#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 |
20816 KB |
Output is correct |
2 |
Correct |
3 ms |
20816 KB |
Output is correct |
3 |
Correct |
4 ms |
20984 KB |
Output is correct |
4 |
Correct |
4 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
20816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20816 KB |
Output is correct |
2 |
Correct |
3 ms |
20816 KB |
Output is correct |
3 |
Correct |
4 ms |
20988 KB |
Output is correct |
4 |
Correct |
5 ms |
20816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20816 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
20952 KB |
Output is correct |
4 |
Correct |
4 ms |
21044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21584 KB |
Output is correct |
2 |
Correct |
7 ms |
21328 KB |
Output is correct |
3 |
Correct |
6 ms |
21584 KB |
Output is correct |
4 |
Correct |
7 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23132 KB |
Output is correct |
2 |
Correct |
13 ms |
22264 KB |
Output is correct |
3 |
Correct |
38 ms |
24216 KB |
Output is correct |
4 |
Correct |
12 ms |
22788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
26308 KB |
Output is correct |
2 |
Correct |
35 ms |
27464 KB |
Output is correct |
3 |
Correct |
45 ms |
29460 KB |
Output is correct |
4 |
Correct |
42 ms |
29512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
36296 KB |
Output is correct |
2 |
Correct |
55 ms |
33096 KB |
Output is correct |
3 |
Correct |
70 ms |
31180 KB |
Output is correct |
4 |
Correct |
50 ms |
29380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
31796 KB |
Output is correct |
2 |
Correct |
73 ms |
33352 KB |
Output is correct |
3 |
Correct |
76 ms |
37704 KB |
Output is correct |
4 |
Correct |
79 ms |
37448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
43444 KB |
Output is correct |
2 |
Correct |
128 ms |
39624 KB |
Output is correct |
3 |
Correct |
145 ms |
39108 KB |
Output is correct |
4 |
Correct |
149 ms |
39084 KB |
Output is correct |
5 |
Correct |
298 ms |
40000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
39360 KB |
Output is correct |
2 |
Correct |
152 ms |
47760 KB |
Output is correct |
3 |
Correct |
217 ms |
45036 KB |
Output is correct |
4 |
Correct |
140 ms |
48448 KB |
Output is correct |
5 |
Correct |
300 ms |
43132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
40396 KB |
Output is correct |
2 |
Correct |
154 ms |
40928 KB |
Output is correct |
3 |
Correct |
219 ms |
41036 KB |
Output is correct |
4 |
Correct |
197 ms |
40520 KB |
Output is correct |
5 |
Correct |
159 ms |
48064 KB |
Output is correct |
6 |
Correct |
332 ms |
43716 KB |
Output is correct |