#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define ld long double
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define int long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 6e5+10 , lg = 48, inf = 1e9+10 , mod = 998244353 ;
int fen[maxn] , n , cnt , sb[maxn] , l[maxn] , r[maxn] ;;
void upd(int x , int w){
while(x <= n+4){
fen[x] += w ;
x += x&-x ;
}
}
int que(int x){
int ans =0 ;
while(x){
ans += fen[x] ;
x -= x&-x ;
}
return ans ;
}
int bui(){
int a;
cin >> a;
if(a != 0){
sb[a] =1 ;
return a;
}
cnt++;
int id = cnt - 1;
l[id] = bui();
r[id] = bui() ;
sb[id] = sb[l[id]] + sb[r[id]];
if(sb[l[id]] < sb[r[id]])swap(l[id],r[id]) ;
return id ;
}
int sm = 0 , ans =0 ;
void rem(int v){
if(v <= n){
upd(v , -1) ;
}else{
rem(l[v]) ;
rem(r[v]) ;
}
}
void add(int v){
if(v <= n){
sm += que(n)-que(v);
}else{
add(l[v]);
add(r[v]);
}
}
void add2(int v){
if(v <= n){
upd(v,1);
}else{
add2(l[v]) ;
add2(r[v]);
}
}
void dfs(int v){
if(v <= n){
upd(v , 1) ;
return ;
}
dfs(r[v]) ;
rem(r[v]) ;
dfs(l[v]) ;
sm = 0;
add(r[v]) ;
add2(r[v]);
ans += min(sm , sb[l[v]] * sb[r[v]] - sm) ;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n ;
cnt = n + 1 ;
int rt= bui() ;
dfs(rt) ;
cout << ans << '\n' ;
return 0 ;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6616 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6620 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7256 KB |
Output is correct |
2 |
Correct |
6 ms |
8672 KB |
Output is correct |
3 |
Correct |
17 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
9308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
8684 KB |
Output is correct |
2 |
Correct |
16 ms |
10076 KB |
Output is correct |
3 |
Correct |
18 ms |
11184 KB |
Output is correct |
4 |
Correct |
20 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
17500 KB |
Output is correct |
2 |
Correct |
23 ms |
16028 KB |
Output is correct |
3 |
Correct |
36 ms |
14772 KB |
Output is correct |
4 |
Correct |
23 ms |
13656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
14936 KB |
Output is correct |
2 |
Correct |
31 ms |
16880 KB |
Output is correct |
3 |
Correct |
33 ms |
19592 KB |
Output is correct |
4 |
Correct |
32 ms |
19284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
19288 KB |
Output is correct |
2 |
Correct |
70 ms |
19536 KB |
Output is correct |
3 |
Correct |
52 ms |
20048 KB |
Output is correct |
4 |
Correct |
68 ms |
19292 KB |
Output is correct |
5 |
Correct |
139 ms |
18404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
17744 KB |
Output is correct |
2 |
Correct |
61 ms |
23896 KB |
Output is correct |
3 |
Correct |
74 ms |
22268 KB |
Output is correct |
4 |
Correct |
55 ms |
24832 KB |
Output is correct |
5 |
Correct |
155 ms |
18268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17496 KB |
Output is correct |
2 |
Correct |
60 ms |
20052 KB |
Output is correct |
3 |
Correct |
103 ms |
18420 KB |
Output is correct |
4 |
Correct |
78 ms |
19280 KB |
Output is correct |
5 |
Correct |
56 ms |
24532 KB |
Output is correct |
6 |
Correct |
165 ms |
18524 KB |
Output is correct |