#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int a[N + 8];
int adj[N + 8][2];
vector<int> _list[N + 8];
int sz[N + 8];
int n, tmp, id = 1, x;
void input() {
cin >> x;
if (x) {_list[id].push_back(x); ++sz[id]; a[id] = x; return;}
int t = id;
adj[t][0] = id + 1;
++id; input();
adj[t][1] = id + 1;
++id; input();
}
void DFS_sz(int u, int p) {
if (!adj[u][0]) return;
DFS_sz(adj[u][0], u); sz[u] += sz[adj[u][0]];
DFS_sz(adj[u][1], u); sz[u] += sz[adj[u][1]];
}
struct Binary_Indexed_Tree {
vector<int> BIT;
Binary_Indexed_Tree() {BIT.assign(N / 2 + 8, 0);}
int pt;
void update(int u, int v) {
pt = u;
while (pt <= N / 2) {
BIT[pt] += v;
pt += pt & -pt;
}
}
int res;
int pf(int p) {
if (p <= 0) return 0;
res = 0; pt = p;
while (pt) {
res += BIT[pt];
pt -= pt & -pt;
}
return res;
}
int range(int l, int r) {
if (l > r) return 0;
return pf(r) - pf(l - 1);
}
} tree;
long long costLR, costRL, ans = 0;
void DFS(int u, int p) {
if (sz[u] > 1) {
int L = adj[u][0], R = adj[u][1];
if (sz[L] < sz[R]) swap(L, R);
// cout << u << " -> " << R << endl;
DFS(R, u);
for (int item : _list[R]) tree.update(item, -1);
// cout << u << " -> " << L << endl;
DFS(L, u);
costLR = costRL = 0;
for (int item : _list[R]) costLR += tree.range(item + 1, n), costRL += tree.range(1, item - 1);
ans += min(costLR, costRL);
// cout << u << ' ' << costLR << ' ' << costRL << '\n';
for (int item : _list[R]) tree.update(item, +1);
for (int v : {L, R}) {
if (_list[u].size() < _list[v].size()) swap(_list[u], _list[v]);
for (int item : _list[v]) _list[u].push_back(item);
}
}
if (a[u]) tree.update(a[u], +1);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n; input();
DFS_sz(1, 0);
DFS(1, 0); cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10840 KB |
Output is correct |
2 |
Correct |
5 ms |
10588 KB |
Output is correct |
3 |
Correct |
5 ms |
10636 KB |
Output is correct |
4 |
Correct |
5 ms |
10588 KB |
Output is correct |
5 |
Correct |
5 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
4 ms |
10588 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10588 KB |
Output is correct |
2 |
Correct |
6 ms |
10588 KB |
Output is correct |
3 |
Correct |
5 ms |
10588 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11100 KB |
Output is correct |
2 |
Correct |
6 ms |
10844 KB |
Output is correct |
3 |
Correct |
6 ms |
11100 KB |
Output is correct |
4 |
Correct |
6 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12380 KB |
Output is correct |
2 |
Correct |
12 ms |
11612 KB |
Output is correct |
3 |
Correct |
30 ms |
13648 KB |
Output is correct |
4 |
Correct |
11 ms |
12376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
15300 KB |
Output is correct |
2 |
Correct |
33 ms |
16816 KB |
Output is correct |
3 |
Correct |
32 ms |
18260 KB |
Output is correct |
4 |
Correct |
33 ms |
18460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
24668 KB |
Output is correct |
2 |
Correct |
39 ms |
21620 KB |
Output is correct |
3 |
Correct |
47 ms |
19924 KB |
Output is correct |
4 |
Correct |
39 ms |
17620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
19680 KB |
Output is correct |
2 |
Correct |
53 ms |
21460 KB |
Output is correct |
3 |
Correct |
51 ms |
25544 KB |
Output is correct |
4 |
Correct |
50 ms |
25428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
31156 KB |
Output is correct |
2 |
Correct |
110 ms |
27596 KB |
Output is correct |
3 |
Correct |
83 ms |
26584 KB |
Output is correct |
4 |
Correct |
104 ms |
26188 KB |
Output is correct |
5 |
Correct |
182 ms |
28380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
26316 KB |
Output is correct |
2 |
Correct |
96 ms |
34380 KB |
Output is correct |
3 |
Correct |
115 ms |
32460 KB |
Output is correct |
4 |
Correct |
84 ms |
35404 KB |
Output is correct |
5 |
Correct |
218 ms |
31056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
27964 KB |
Output is correct |
2 |
Correct |
89 ms |
28872 KB |
Output is correct |
3 |
Correct |
161 ms |
29512 KB |
Output is correct |
4 |
Correct |
109 ms |
28552 KB |
Output is correct |
5 |
Correct |
88 ms |
36040 KB |
Output is correct |
6 |
Correct |
198 ms |
33236 KB |
Output is correct |