#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 1e6 + 100;
const ll INF = (ll)2e18;
const int inf = (ll)2e9;
const int maxl = 26;
const int MOD = 1e9 + 7;
int n, szv;
int L[maxn];
int R[maxn];
int sz[maxn];
int cnt[maxn];
int t[maxn];
ll dp[maxn];
void calc(int v){
if(v <= n){
sz[v] = 1;
cnt[v] = 1;
return;
}
calc(L[v]);
calc(R[v]);
sz[v] = sz[L[v]] + sz[R[v]] + 1;
cnt[v] = cnt[L[v]] + cnt[R[v]];
}
void upd(int i, int x){
for(; i <= n; i |= (i + 1)) t[i] += x;
}
int get(int i){
int res = 0;
for(; i > 0; i = (i & (i + 1)) - 1) res += t[i];
return res;
}
ll ans(int v){
if(v <= n) return get(v);
return ans(L[v]) + ans(R[v]);
}
void updv(int v, int d){
if(v <= n) upd(v, d);
else updv(L[v], d), updv(R[v], d);
}
void dfs(int v, int clear){
if(v <= n){
if(clear) return;
upd(v, 1);
} else{
if(sz[L[v]] < sz[R[v]]) swap(L[v], R[v]);
dfs(R[v], 1); dfs(L[v], 0);
ll cal = ans(R[v]);
dp[v] = dp[L[v]] + dp[R[v]] + min(cal, 1ll * cnt[L[v]] * cnt[R[v]] - cal);
if(!clear) updv(R[v], 1);
else updv(L[v], -1);
}
}
int rec(){
int x; cin >> x;
if(!x){
int v = ++szv;
L[v] = rec();
R[v] = rec();
return v;
} else return x;
}
void test(){
cin >> n;
szv = n;
rec();
calc(n + 1);
dfs(n + 1, 0);
cout << dp[n + 1];
}
int main(){
// freopen("cows.in", "r", stdin);
// freopen("cows.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t; t = 1;
while(t--) test();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
5 ms |
832 KB |
Output is correct |
3 |
Correct |
14 ms |
1560 KB |
Output is correct |
4 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2088 KB |
Output is correct |
2 |
Correct |
13 ms |
3280 KB |
Output is correct |
3 |
Correct |
16 ms |
4200 KB |
Output is correct |
4 |
Correct |
15 ms |
4240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
6868 KB |
Output is correct |
2 |
Correct |
21 ms |
5980 KB |
Output is correct |
3 |
Correct |
26 ms |
4812 KB |
Output is correct |
4 |
Correct |
21 ms |
4348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4664 KB |
Output is correct |
2 |
Correct |
28 ms |
6392 KB |
Output is correct |
3 |
Correct |
29 ms |
8400 KB |
Output is correct |
4 |
Correct |
28 ms |
8160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
8360 KB |
Output is correct |
2 |
Correct |
55 ms |
8932 KB |
Output is correct |
3 |
Correct |
48 ms |
9000 KB |
Output is correct |
4 |
Correct |
58 ms |
8708 KB |
Output is correct |
5 |
Correct |
88 ms |
7748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
7892 KB |
Output is correct |
2 |
Correct |
54 ms |
12816 KB |
Output is correct |
3 |
Correct |
62 ms |
11504 KB |
Output is correct |
4 |
Correct |
46 ms |
13388 KB |
Output is correct |
5 |
Correct |
101 ms |
8712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
7984 KB |
Output is correct |
2 |
Correct |
49 ms |
10164 KB |
Output is correct |
3 |
Correct |
89 ms |
9116 KB |
Output is correct |
4 |
Correct |
59 ms |
9556 KB |
Output is correct |
5 |
Correct |
44 ms |
13752 KB |
Output is correct |
6 |
Correct |
92 ms |
9036 KB |
Output is correct |