#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 4e5 + 5;
int n, t, a[NMAX], lc[NMAX], rc[NMAX];
int rt[NMAX], l[NMAX * 20], r[NMAX * 20], s[NMAX * 20], cnt;
ll ans, r1, r2;
void dfs(int x){
cin >> a[x];
if(!a[x]){
lc[x] = ++t; dfs(lc[x]);
rc[x] = ++t; dfs(rc[x]);
}
return;
}
int upd(int le, int ri, int k){
int ix = ++cnt;
s[ix] = 1;
if(le == ri) return ix;
int m = (le + ri) / 2;
if(k <= m) l[ix] = upd(le, m, k);
else r[ix] = upd(m + 1, ri, k);
return ix;
}
int merge(int x, int y){
if(!x) return y;
if(!y) return x;
r1 += s[l[x]] * s[r[y]];
r2 += s[l[y]] * s[r[x]];
l[x] = merge(l[x], l[y]);
r[x] = merge(r[x], r[y]);
s[x] = s[l[x]] + s[r[x]];
return x;
}
ll dfs2(int x){
if(a[x]) return 0;
ll ret = dfs2(lc[x]) + dfs2(rc[x]);
r1 = r2 = 0;
rt[x] = merge(rt[lc[x]], rt[rc[x]]);
ret += min(r1, r2);
return ret;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
dfs(++t);
for(int i = 1; i <= t; i++)
if(a[i]) rt[i] = upd(1, n, a[i]);
cout << dfs2(1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3028 KB |
Output is correct |
2 |
Correct |
11 ms |
2920 KB |
Output is correct |
3 |
Correct |
21 ms |
6868 KB |
Output is correct |
4 |
Correct |
10 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11860 KB |
Output is correct |
2 |
Correct |
35 ms |
11992 KB |
Output is correct |
3 |
Correct |
39 ms |
13848 KB |
Output is correct |
4 |
Correct |
43 ms |
14832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
22776 KB |
Output is correct |
2 |
Correct |
74 ms |
22048 KB |
Output is correct |
3 |
Correct |
92 ms |
20780 KB |
Output is correct |
4 |
Correct |
57 ms |
21288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
29636 KB |
Output is correct |
2 |
Correct |
123 ms |
30420 KB |
Output is correct |
3 |
Correct |
90 ms |
32712 KB |
Output is correct |
4 |
Correct |
101 ms |
32424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
44752 KB |
Output is correct |
2 |
Correct |
109 ms |
45856 KB |
Output is correct |
3 |
Correct |
184 ms |
44780 KB |
Output is correct |
4 |
Correct |
132 ms |
45388 KB |
Output is correct |
5 |
Correct |
155 ms |
43908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
48156 KB |
Output is correct |
2 |
Incorrect |
198 ms |
53688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
210 ms |
50448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |