#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;
ll 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, ll 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 |
12 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23904 KB |
Output is correct |
3 |
Correct |
13 ms |
23844 KB |
Output is correct |
4 |
Correct |
14 ms |
23896 KB |
Output is correct |
5 |
Correct |
13 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23864 KB |
Output is correct |
2 |
Correct |
14 ms |
23908 KB |
Output is correct |
3 |
Correct |
13 ms |
23892 KB |
Output is correct |
4 |
Correct |
14 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
13 ms |
23880 KB |
Output is correct |
3 |
Correct |
13 ms |
23892 KB |
Output is correct |
4 |
Correct |
13 ms |
23932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24276 KB |
Output is correct |
2 |
Correct |
18 ms |
23988 KB |
Output is correct |
3 |
Correct |
14 ms |
24276 KB |
Output is correct |
4 |
Correct |
14 ms |
24216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
25012 KB |
Output is correct |
2 |
Correct |
25 ms |
24404 KB |
Output is correct |
3 |
Correct |
56 ms |
25300 KB |
Output is correct |
4 |
Correct |
21 ms |
24972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
26232 KB |
Output is correct |
2 |
Correct |
47 ms |
27220 KB |
Output is correct |
3 |
Correct |
49 ms |
28372 KB |
Output is correct |
4 |
Correct |
48 ms |
28468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
32336 KB |
Output is correct |
2 |
Correct |
61 ms |
30436 KB |
Output is correct |
3 |
Correct |
79 ms |
29120 KB |
Output is correct |
4 |
Correct |
59 ms |
28224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
29576 KB |
Output is correct |
2 |
Correct |
75 ms |
30704 KB |
Output is correct |
3 |
Incorrect |
76 ms |
32932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
248 ms |
34392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
145 ms |
33424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
163 ms |
33444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |