#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 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 |
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 |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
388 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 |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 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 |
3 ms |
1236 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3028 KB |
Output is correct |
2 |
Correct |
11 ms |
2820 KB |
Output is correct |
3 |
Correct |
20 ms |
6868 KB |
Output is correct |
4 |
Correct |
8 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
1620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
15864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
7132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
14504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
14580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
28 ms |
4072 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |