//Bismillahir-Rahmanir-Rahim
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
# define pii pair <int, int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e6 + 2;
const ll inf = 2e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, nv;
ll L[maxn];
ll R[maxn];
ll sz[maxn];
ll cnt[maxn];
ll t[maxn];
ll dp[maxn];
void upd (ll id, ll x)
{
for (; id < maxn; id |= (id + 1))
{
t[id] += x;
}
}
ll get (ll id)
{
ll res = 0;
for (; id >= 1; id = (id & (id + 1)) - 1)
{
res += t[id];
}
return res;
}
void calc (ll v = n + 1)
{
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 updv (ll v, ll x)
{
if (v <= n)
{
upd(v, x);
return;
}
updv(L[v], x);
updv(R[v], x);
}
ll count (ll v)
{
if (v <= n) return get(v);
return count(L[v]) + count(R[v]);
}
void dfs (ll v = n + 1, bool keep = 1)
{
if (v <= n)
{
if (keep)
{
updv(v, 1);
}
return;
}
if (sz[L[v]] < sz[R[v]])
{
swap(L[v], R[v]);
}
dfs(R[v], 0); dfs(L[v], 1);
ll inv = count(R[v]);
dp[v] = dp[L[v]] + dp[R[v]] + min(inv, cnt[L[v]] * 1LL * cnt[R[v]] - inv);
if (keep)
{
updv(R[v], 1);
}
else
{
updv(L[v], -1);
}
}
ll rec ()
{
ll x; cin >> x;
if (!x)
{
ll v = ++nv;
L[v] = rec ();
R[v] = rec ();
return v;
}
return x;
}
void ma1n (/* SABR */)
{
cin >> n;
nv = n;
rec ();
calc ();
dfs ();
cout << dp[n + 1];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ll ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << '\n';
ma1n();
}
return 0;
}
// 998batrr | BbIWEJI 3A TObOU!!!
// tourist | BbIWEJI 3A TObOU!!!
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1492 KB |
Output is correct |
2 |
Correct |
7 ms |
1204 KB |
Output is correct |
3 |
Correct |
15 ms |
2184 KB |
Output is correct |
4 |
Correct |
5 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
3540 KB |
Output is correct |
2 |
Correct |
15 ms |
4240 KB |
Output is correct |
3 |
Correct |
19 ms |
5264 KB |
Output is correct |
4 |
Correct |
19 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
9136 KB |
Output is correct |
2 |
Correct |
25 ms |
7636 KB |
Output is correct |
3 |
Correct |
31 ms |
6492 KB |
Output is correct |
4 |
Correct |
23 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
8020 KB |
Output is correct |
2 |
Correct |
34 ms |
8652 KB |
Output is correct |
3 |
Correct |
33 ms |
10740 KB |
Output is correct |
4 |
Correct |
31 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
13008 KB |
Output is correct |
2 |
Correct |
64 ms |
12280 KB |
Output is correct |
3 |
Correct |
59 ms |
12220 KB |
Output is correct |
4 |
Correct |
68 ms |
12004 KB |
Output is correct |
5 |
Correct |
105 ms |
10992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
13064 KB |
Output is correct |
2 |
Correct |
66 ms |
16428 KB |
Output is correct |
3 |
Correct |
79 ms |
15096 KB |
Output is correct |
4 |
Correct |
56 ms |
17100 KB |
Output is correct |
5 |
Correct |
129 ms |
12416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
13476 KB |
Output is correct |
2 |
Correct |
60 ms |
14052 KB |
Output is correct |
3 |
Correct |
112 ms |
12992 KB |
Output is correct |
4 |
Correct |
73 ms |
13456 KB |
Output is correct |
5 |
Correct |
53 ms |
17620 KB |
Output is correct |
6 |
Correct |
129 ms |
12904 KB |
Output is correct |