#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ff(x,a,b,c) for (auto x=a;x<=b;x+=c)
#define fd(x,a,b,c) for (auto x=a;x>=b;x-=c)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
const int N = 1e6+5;
const int mod = 1e9+7;
const int INF = 1e18;
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,idx,sz[N],l[N],r[N],bit[N],sum = 0,ans = 0;
//fenwick tree
void update(int x,int w)
{
for (;x<=n+4;x += x&-x) bit[x] += w;
}
int get(int x)
{
int res = 0;
for (;x>0;x -= x & -x) res += bit[x];
return res;
}
void push(int u)
{
if (u<=n)
{
update(u,-1); return;
}
push(l[u]); push(r[u]);
}
void cal(int u)
{
if (u<=n)
{
sum += get(n)-get(u); return;
}
cal(l[u]); cal(r[u]);
}
void push1(int u)
{
if (u<=n)
{
update(u,1); return;
}
push1(l[u]); push1(r[u]);
}
void dfs(int u)
{
if (u<=n)
{
update(u,1); return;
}
//cout<<r[u]<<" "<<l[u]<<endl;
dfs(r[u]);
push(r[u]);
dfs(l[u]);
sum = 0;
cal(r[u]); push1(r[u]);
ans += min(sum, sz[l[u]]*sz[r[u]]-sum);
}
int init()
{
int x; cin>>x;
if(x){
sz[x] =1 ;
return x;
}
idx++;
int i = idx - 1;
l[i] = init();
r[i] = init();
//cout<<i<<endl<<l[i]<<" "<<r[i]<<endl;
sz[i] = sz[l[i]] + sz[r[i]];
if(sz[l[i]] < sz[r[i]])swap(l[i],r[i]) ;
return i;
}
void solve()
{
cin>>n;
idx = n+1;
int root = init();
//cout<<root<<endl;
dfs(root);
cout<<ans;
}
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
//cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
736 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1688 KB |
Output is correct |
2 |
Correct |
5 ms |
860 KB |
Output is correct |
3 |
Correct |
16 ms |
1884 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2652 KB |
Output is correct |
2 |
Correct |
15 ms |
3984 KB |
Output is correct |
3 |
Correct |
16 ms |
5080 KB |
Output is correct |
4 |
Correct |
18 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
9104 KB |
Output is correct |
2 |
Correct |
23 ms |
7004 KB |
Output is correct |
3 |
Correct |
30 ms |
5428 KB |
Output is correct |
4 |
Correct |
23 ms |
4620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
5980 KB |
Output is correct |
2 |
Correct |
30 ms |
7004 KB |
Output is correct |
3 |
Correct |
32 ms |
9560 KB |
Output is correct |
4 |
Correct |
32 ms |
9508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
10636 KB |
Output is correct |
2 |
Correct |
81 ms |
9624 KB |
Output is correct |
3 |
Correct |
52 ms |
9928 KB |
Output is correct |
4 |
Correct |
66 ms |
9348 KB |
Output is correct |
5 |
Correct |
113 ms |
8016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
9808 KB |
Output is correct |
2 |
Correct |
65 ms |
14512 KB |
Output is correct |
3 |
Correct |
75 ms |
12696 KB |
Output is correct |
4 |
Correct |
55 ms |
15140 KB |
Output is correct |
5 |
Correct |
168 ms |
9040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10068 KB |
Output is correct |
2 |
Correct |
62 ms |
10844 KB |
Output is correct |
3 |
Correct |
103 ms |
9368 KB |
Output is correct |
4 |
Correct |
82 ms |
10100 KB |
Output is correct |
5 |
Correct |
68 ms |
15400 KB |
Output is correct |
6 |
Correct |
182 ms |
9296 KB |
Output is correct |