#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=4e5+5;
ll n, cnt, t, x, sz[nx], p[nx], c;
vector<ll> d[nx];
struct fenwick
{
ll d[nx];
void add(int i, int vl)
{
while (i<nx) d[i]+=vl, i+=(i&-i);
}
int query(int i)
{
int res=0;
while (i>0) res+=d[i], i-=(i&-i);
return res;
}
} f;
void dfssz(int u)
{
sz[u]=1;
for (auto v:d[u]) dfssz(v), sz[u]+=sz[v];
}
pair<ll, ll> dfsvl(int u)
{
if (u<=n) return make_pair(f.query(u-1), f.query(nx-1)-f.query(u));
auto l=dfsvl(d[u][0]), r=dfsvl(d[u][1]);
return make_pair(l.first+r.first, l.second+r.second);
}
void dfsrem(int u)
{
if (u<=n) return f.add(u, -1), void();
dfsrem(d[u][0]);
dfsrem(d[u][1]);
}
void dfsadd(int u)
{
if (u<=n) return f.add(u, 1), void();
dfsadd(d[u][0]);
dfsadd(d[u][1]);
}
ll solve(int u, bool del)
{
if (u<=n&&!del) f.add(u, 1);
if (u<=n) return 0;
if (sz[d[u][0]]<sz[d[u][1]]) swap(d[u][0], d[u][1]);
ll res=solve(d[u][1], 1)+solve(d[u][0], 0);
//if (u==8) cout<<f.query(0)<<' '<<f.query(1)<<' '<<f.query(5)<<'\n';
auto tmp=dfsvl(d[u][1]);
if (del) dfsrem(d[u][0]);
else dfsadd(d[u][1]);
//cout<<u<<' '<<del<<' '<<res<<' '<<tmp.first<<' '<<tmp.second<<' '<<min(tmp.first, tmp.second)<<'\n';
return res+min(tmp.first, tmp.second);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>x;
t=c=n+1;
while (cnt!=n)
{
cin>>x;
if (!x) d[c].push_back(++t), p[t]=c, c=t;
else
{
d[c].push_back(x); cnt++;
while (d[c].size()==2) c=p[c];
}
}
dfssz(n+1);
cout<<solve(n+1, 0);
}
/*
5
0
0
0
5
1
2
0
4
3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16736 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16932 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
5 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
17752 KB |
Output is correct |
2 |
Correct |
11 ms |
17364 KB |
Output is correct |
3 |
Correct |
24 ms |
17756 KB |
Output is correct |
4 |
Correct |
8 ms |
17500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
18524 KB |
Output is correct |
2 |
Correct |
23 ms |
19288 KB |
Output is correct |
3 |
Correct |
28 ms |
20060 KB |
Output is correct |
4 |
Correct |
26 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
25168 KB |
Output is correct |
2 |
Correct |
34 ms |
23644 KB |
Output is correct |
3 |
Correct |
44 ms |
22620 KB |
Output is correct |
4 |
Correct |
36 ms |
22104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
22872 KB |
Output is correct |
2 |
Correct |
53 ms |
24916 KB |
Output is correct |
3 |
Correct |
47 ms |
26568 KB |
Output is correct |
4 |
Correct |
47 ms |
26452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
26196 KB |
Output is correct |
2 |
Correct |
98 ms |
27000 KB |
Output is correct |
3 |
Correct |
82 ms |
26964 KB |
Output is correct |
4 |
Correct |
95 ms |
26760 KB |
Output is correct |
5 |
Correct |
143 ms |
25684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
25868 KB |
Output is correct |
2 |
Correct |
88 ms |
30800 KB |
Output is correct |
3 |
Correct |
109 ms |
29372 KB |
Output is correct |
4 |
Correct |
79 ms |
31316 KB |
Output is correct |
5 |
Correct |
180 ms |
26816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
25940 KB |
Output is correct |
2 |
Correct |
87 ms |
28120 KB |
Output is correct |
3 |
Correct |
151 ms |
27168 KB |
Output is correct |
4 |
Correct |
102 ms |
27728 KB |
Output is correct |
5 |
Correct |
81 ms |
31832 KB |
Output is correct |
6 |
Correct |
171 ms |
27384 KB |
Output is correct |