#include <bits/stdc++.h>
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REV(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ll long long
#define fi first
#define se second
#define int long long
using namespace std;
const int N = 2e5 + 5;
int cnt, node;
int n;
int l[2 * N], r[2 * N];
int st[2 * N], ed[2 * N];
int a[N];
struct BIT {
int tree[N];
void add(int x, int v){
while(x <= n){
tree[x] += v;
x += x & -x;
}
}
int query(int x){
int ret = 0;
while(x) ret += tree[x], x -= x & -x;
return ret;
}
} bit;
void read_tree(int v){
int x; cin >> x;
st[v] = cnt;
if(x == 0){
l[v] = ++node;
read_tree(node);
r[v] = ++node;
read_tree(node);
}
else a[cnt++] = x;
ed[v] = cnt;
}
int dfs(int v){
if(ed[v] - st[v] == 1){
bit.add(a[st[v]], 1);
return 0;
}
int ls = ed[l[v]] - st[l[v]];
int rs = ed[r[v]] - st[r[v]];
int ans1 = 0, ans2 = 0;
if(ls < rs){
ans1 += dfs(l[v]);
FOR(i, st[l[v]], ed[l[v]] - 1) {
bit.add(a[i], -1);
}
ans1 += dfs(r[v]);
FOR(i, st[l[v]], ed[l[v]] - 1) {
ans2 += bit.query(a[i]);
}
FOR(i, st[l[v]], ed[l[v]] - 1) {
bit.add(a[i], 1);
}
}
else {
ans1 += dfs(r[v]);
FOR(i, st[r[v]], ed[r[v]] - 1) {
bit.add(a[i], -1);
}
ans1 += dfs(l[v]);
FOR(i, st[r[v]], ed[r[v]] - 1) {
ans2 += ls - bit.query(a[i]);
}
FOR(i, st[r[v]], ed[r[v]] - 1) {
bit.add(a[i], 1);
}
}
return ans1 + min(ls * rs - ans2, ans2);
}
void solve(int tc) {
cin >> n;
read_tree(0);
cout << dfs(0) << '\n';
return;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for(int i = 1; i <= tc; ++i) solve(tc);
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 |
468 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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1876 KB |
Output is correct |
2 |
Correct |
4 ms |
1372 KB |
Output is correct |
3 |
Correct |
15 ms |
3088 KB |
Output is correct |
4 |
Correct |
4 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4696 KB |
Output is correct |
2 |
Correct |
14 ms |
5464 KB |
Output is correct |
3 |
Correct |
15 ms |
6864 KB |
Output is correct |
4 |
Correct |
17 ms |
7012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
11612 KB |
Output is correct |
2 |
Correct |
26 ms |
10060 KB |
Output is correct |
3 |
Correct |
27 ms |
8760 KB |
Output is correct |
4 |
Correct |
21 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
10584 KB |
Output is correct |
2 |
Correct |
32 ms |
11868 KB |
Output is correct |
3 |
Correct |
30 ms |
13636 KB |
Output is correct |
4 |
Correct |
31 ms |
13652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
17500 KB |
Output is correct |
2 |
Correct |
64 ms |
16472 KB |
Output is correct |
3 |
Correct |
53 ms |
16472 KB |
Output is correct |
4 |
Correct |
64 ms |
15700 KB |
Output is correct |
5 |
Correct |
108 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
16976 KB |
Output is correct |
2 |
Correct |
61 ms |
21844 KB |
Output is correct |
3 |
Correct |
72 ms |
20504 KB |
Output is correct |
4 |
Correct |
51 ms |
22608 KB |
Output is correct |
5 |
Correct |
137 ms |
17252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
17064 KB |
Output is correct |
2 |
Correct |
64 ms |
19324 KB |
Output is correct |
3 |
Correct |
102 ms |
17800 KB |
Output is correct |
4 |
Correct |
75 ms |
17492 KB |
Output is correct |
5 |
Correct |
52 ms |
23888 KB |
Output is correct |
6 |
Correct |
164 ms |
17748 KB |
Output is correct |