//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 = 2e5 + 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;
int n, nv;
int L[maxn * 2];
int R[maxn * 2];
int sz[maxn * 2];
int cnt[maxn * 2];
ll t[maxn], ans;
void upd (int id, int x)
{
for (; id < maxn; id += (id & (-id)))
{
t[id] += x;
}
}
int get (int id)
{
int res = 0;
for (; id >= 1; id -= (id & (-id)))
{
res += t[id];
}
return res;
}
int rec ()
{
int x; cin >> x;
if (!x)
{
int v = ++nv;
L[v] = rec ();
R[v] = rec ();
sz[v] = sz[L[v]] + sz[R[v]] + 1;
cnt[v] = cnt[L[v]] + cnt[R[v]];
return v;
}
sz[x] = 1, cnt[x] = 1;
return x;
}
void add (int v)
{
if (v <= n) upd(v, 1);
else add(L[v]), add(R[v]);
}
void clr (int v)
{
if (v <= n) upd(v, -1);
else clr(L[v]), clr(R[v]);
}
ll calc (int v)
{
if (v <= n) return get(v);
return calc(L[v]) + calc(R[v]);
}
void dfs (int v = n + 1, bool keep = 1)
{
if (v <= n)
{
add(v);
}
else
{
if (sz[L[v]] < sz[R[v]])
{
swap(L[v], R[v]);
}
dfs(R[v], 0);
dfs(L[v], 1);
ll inv = calc(R[v]);
// cout << v << ' ' << inv << ' ' << cnt[L[v]] << ' ' << cnt[R[v]] << nl;
ans += min(1LL * inv, cnt[L[v]] * 1LL * cnt[R[v]] - inv);
add(R[v]);
}
if (!keep)
{
clr(v);
}
}
void ma1n (/* SABR */)
{
cin >> n;
nv = n;
rec ();
dfs ();
cout << ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
int 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
6 ms |
724 KB |
Output is correct |
3 |
Correct |
18 ms |
1308 KB |
Output is correct |
4 |
Correct |
5 ms |
1128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
1876 KB |
Output is correct |
2 |
Correct |
16 ms |
2796 KB |
Output is correct |
3 |
Correct |
22 ms |
3608 KB |
Output is correct |
4 |
Correct |
18 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6620 KB |
Output is correct |
2 |
Correct |
23 ms |
4948 KB |
Output is correct |
3 |
Correct |
32 ms |
3924 KB |
Output is correct |
4 |
Correct |
24 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4192 KB |
Output is correct |
2 |
Correct |
31 ms |
5000 KB |
Output is correct |
3 |
Correct |
32 ms |
6868 KB |
Output is correct |
4 |
Correct |
29 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
7552 KB |
Output is correct |
2 |
Correct |
70 ms |
6704 KB |
Output is correct |
3 |
Correct |
58 ms |
7016 KB |
Output is correct |
4 |
Correct |
68 ms |
6576 KB |
Output is correct |
5 |
Correct |
126 ms |
5664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
7116 KB |
Output is correct |
2 |
Correct |
66 ms |
10452 KB |
Output is correct |
3 |
Correct |
73 ms |
9112 KB |
Output is correct |
4 |
Correct |
52 ms |
11096 KB |
Output is correct |
5 |
Correct |
142 ms |
6412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
7272 KB |
Output is correct |
2 |
Correct |
56 ms |
7768 KB |
Output is correct |
3 |
Correct |
126 ms |
6580 KB |
Output is correct |
4 |
Correct |
82 ms |
7156 KB |
Output is correct |
5 |
Correct |
52 ms |
11264 KB |
Output is correct |
6 |
Correct |
141 ms |
6576 KB |
Output is correct |