#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
using ll = long long;
const int N = 1e6 + 123;
int l[N], r[N], temp, sz[N], cnt[N];
int build() {
int v = temp++;
int x;
cin >> x;
if (x) {
sz[v] = cnt[v] = 1;
l[v] = r[v] = x;
// cout << v << " " << l[v] << " " << r[v] << endl;
return v;
}
l[v] = build();
r[v] = build();
sz[v] = sz[l[v]] + sz[r[v]] + 1;
cnt[v] = cnt[l[v]] + cnt[r[v]];
// cout << v << " " << l[v] << " " << r[v] << endl;
return v;
}
int f[N];
void upd(int v, int t) {
while (v < N) f[v] += t, v |= v + 1;
}
int get(int v) {
int r = 0;
while (v >= 0) r += f[v], v &= v + 1, v--;
return r;
}
void clr(int v) {
if (l[v] == r[v]) upd(l[v], -1);
else clr(l[v]), clr(r[v]);
}
ll ans, res;
void calc(int v) {
if (l[v] == r[v]) res += get(l[v]);
else calc(l[v]), calc(r[v]);
}
void add(int v) {
if (l[v] == r[v]) upd(l[v], +1);
else add(l[v]), add(r[v]);
}
void dfs(int v, int type) {
// return;
// cout << v << endl;
if (l[v] != r[v]) {
if (sz[l[v]] > sz[r[v]]) {
dfs(r[v], 1);
dfs(l[v], 0);
res = 0;
calc(r[v]);
add(r[v]);
} else {
dfs(l[v], 1);
dfs(r[v], 0);
res = 0;
calc(l[v]);
add(l[v]);
}
ll cost = min(cnt[l[v]] * 1ll * cnt[r[v]] - res, 1ll * res);
// cout << v << " " << cost << " " << temp << " cost" << endl;
ans += cost;
} else {
upd(l[v], +1);
}
if (type == 1) {
clr(v);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
dfs(build(), 0);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 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 |
1 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 |
1236 KB |
Output is correct |
2 |
Correct |
7 ms |
724 KB |
Output is correct |
3 |
Correct |
21 ms |
1436 KB |
Output is correct |
4 |
Correct |
5 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2180 KB |
Output is correct |
2 |
Correct |
18 ms |
2900 KB |
Output is correct |
3 |
Correct |
20 ms |
3836 KB |
Output is correct |
4 |
Correct |
21 ms |
3848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6952 KB |
Output is correct |
2 |
Correct |
26 ms |
5332 KB |
Output is correct |
3 |
Correct |
34 ms |
4256 KB |
Output is correct |
4 |
Correct |
29 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
4696 KB |
Output is correct |
2 |
Correct |
34 ms |
5484 KB |
Output is correct |
3 |
Correct |
36 ms |
7400 KB |
Output is correct |
4 |
Correct |
34 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
8368 KB |
Output is correct |
2 |
Correct |
77 ms |
7504 KB |
Output is correct |
3 |
Correct |
62 ms |
7692 KB |
Output is correct |
4 |
Correct |
75 ms |
7264 KB |
Output is correct |
5 |
Correct |
128 ms |
6356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
7900 KB |
Output is correct |
2 |
Correct |
69 ms |
11264 KB |
Output is correct |
3 |
Correct |
78 ms |
9868 KB |
Output is correct |
4 |
Correct |
58 ms |
11868 KB |
Output is correct |
5 |
Correct |
156 ms |
7196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
7996 KB |
Output is correct |
2 |
Correct |
61 ms |
8524 KB |
Output is correct |
3 |
Correct |
114 ms |
7352 KB |
Output is correct |
4 |
Correct |
96 ms |
7964 KB |
Output is correct |
5 |
Correct |
64 ms |
12136 KB |
Output is correct |
6 |
Correct |
154 ms |
7360 KB |
Output is correct |