#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e6 + 5;
const int base = 1 << 20;
int n, a[NMAX], lc[NMAX], rc[NMAX], w[NMAX], t;
int seg[1 << 21];
vector<int> adj[NMAX];
ll ans;
void dfs(int x){
cin >> a[x];
if(a[x]) w[x] = 1;
else{
lc[x] = ++t; dfs(lc[x]);
rc[x] = ++t; dfs(rc[x]);
if(w[lc[x]] > w[rc[x]]) swap(lc[x], rc[x]);
w[x] = w[lc[x]] + w[rc[x]];
}
return;
}
void upd(int idx, int v){
idx += base;
while(idx){
seg[idx] += v; idx /= 2;
}
return;
}
ll sum(int l, int r){
ll ret = 0;
l += base; r += base;
while(l <= r){
if(l & 1) ret += seg[l++];
if(!(r & 1)) ret += seg[r--];
l /= 2; r /= 2;
}
return ret;
}
void f(int x, int v){
if(a[x]){
upd(a[x], v); return;
}
f(lc[x], v);
f(rc[x], v);
return;
}
ll cal(int x){
if(a[x]) return sum(a[x] + 1, base - 1);
ll ret = cal(lc[x]);
ret += cal(rc[x]);
return ret;
}
void dfs2(int x){
if(a[x]) {
upd(a[x], 1);
return;
}
ll v = 0;
dfs2(lc[x]);
f(lc[x], -1);
dfs2(rc[x]);
v += cal(lc[x]);
f(lc[x], 1);
ans += min(v, w[lc[x]] * w[rc[x]] - v);
return;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
dfs(++t);
dfs2(1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23812 KB |
Output is correct |
2 |
Correct |
13 ms |
23824 KB |
Output is correct |
3 |
Correct |
14 ms |
23872 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
16 ms |
23804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23860 KB |
Output is correct |
2 |
Correct |
16 ms |
23900 KB |
Output is correct |
3 |
Correct |
15 ms |
23784 KB |
Output is correct |
4 |
Correct |
14 ms |
23780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23908 KB |
Output is correct |
2 |
Correct |
17 ms |
23912 KB |
Output is correct |
3 |
Correct |
17 ms |
23884 KB |
Output is correct |
4 |
Correct |
16 ms |
23964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24276 KB |
Output is correct |
2 |
Correct |
21 ms |
24084 KB |
Output is correct |
3 |
Correct |
16 ms |
24208 KB |
Output is correct |
4 |
Correct |
18 ms |
24204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24968 KB |
Output is correct |
2 |
Correct |
44 ms |
24464 KB |
Output is correct |
3 |
Correct |
64 ms |
25292 KB |
Output is correct |
4 |
Correct |
26 ms |
24908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
26268 KB |
Output is correct |
2 |
Correct |
82 ms |
27148 KB |
Output is correct |
3 |
Correct |
57 ms |
28344 KB |
Output is correct |
4 |
Correct |
52 ms |
28468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
32320 KB |
Output is correct |
2 |
Correct |
79 ms |
30428 KB |
Output is correct |
3 |
Correct |
98 ms |
29000 KB |
Output is correct |
4 |
Correct |
69 ms |
28252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
29684 KB |
Output is correct |
2 |
Correct |
89 ms |
30708 KB |
Output is correct |
3 |
Incorrect |
106 ms |
32968 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
288 ms |
34436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
160 ms |
33532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
189 ms |
33524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |