//Src : Vux2Code
#include <bits/stdc++.h>
using namespace std;
typedef int ll ;
const ll N = 10000005;
ll sum[N], ls[N], rs[N];
ll root[N], l[N], r[N], val[N];
ll ans, s1, s2;
ll cnt, n, seg;
void init(ll ro) {
cin >> val[ro];
if (!val[ro]) {
l[ro] = ++cnt;
init(l[ro]);
r[ro] = ++cnt;
init(r[ro]);
}
}
void pushup(ll rt) {
sum[rt] = sum[ls[rt]] + sum[rs[rt]];
}
void build(ll l, ll r, ll &rt, ll key) {
if (!rt) rt = ++seg;
if (l == r) {
sum[rt]++;
return;
}
ll mid = (l + r) >> 1;
if (key <= mid) build(l, mid, ls[rt], key);
else build(mid + 1, r, rs[rt], key);
pushup(rt);
}
ll merge(ll r1, ll r2) {
if (!r1 || !r2) return r1 + r2;
s1 += (ll)sum[rs[r1]] * sum[ls[r2]];
s2 += (ll)sum[ls[r1]] * sum[rs[r2]];
ls[r1] = merge(ls[r1], ls[r2]);
rs[r1] = merge(rs[r1], rs[r2]);
pushup(r1);
return r1;
}
void solve(ll ro) {
if (!ro) return;
solve(l[ro]);
solve(r[ro]);
if (!val[ro]) {
s1 = s2 = 0;
root[ro] = merge(root[l[ro]], root[r[ro]]);
ans += min(s1, s2);
}
}
int main() {
ios::sync_with_stdio (0);
cin. tie (0);
cout. tie (0);
cin >> n;
cnt = 1;
init(1);
for (ll i = 1; i <= cnt; ++i)
if (val[i]) build(1, n, root[i], val[i]);
solve(1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
3 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1372 KB |
Output is correct |
4 |
Correct |
2 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2904 KB |
Output is correct |
2 |
Correct |
6 ms |
2908 KB |
Output is correct |
3 |
Correct |
19 ms |
7092 KB |
Output is correct |
4 |
Correct |
7 ms |
3192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
11868 KB |
Output is correct |
2 |
Correct |
22 ms |
11840 KB |
Output is correct |
3 |
Correct |
30 ms |
13404 KB |
Output is correct |
4 |
Correct |
25 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
21724 KB |
Output is correct |
2 |
Correct |
59 ms |
21416 KB |
Output is correct |
3 |
Correct |
47 ms |
20568 KB |
Output is correct |
4 |
Correct |
43 ms |
21080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
29524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
44112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
120 ms |
47956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
125 ms |
50260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |