#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);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10748 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10832 KB |
Output is correct |
2 |
Correct |
3 ms |
10576 KB |
Output is correct |
3 |
Correct |
3 ms |
10832 KB |
Output is correct |
4 |
Correct |
2 ms |
10832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13392 KB |
Output is correct |
2 |
Correct |
5 ms |
12880 KB |
Output is correct |
3 |
Correct |
15 ms |
12964 KB |
Output is correct |
4 |
Correct |
5 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
12880 KB |
Output is correct |
2 |
Correct |
15 ms |
14328 KB |
Output is correct |
3 |
Correct |
17 ms |
14928 KB |
Output is correct |
4 |
Correct |
17 ms |
14672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
17744 KB |
Output is correct |
2 |
Correct |
25 ms |
17740 KB |
Output is correct |
3 |
Correct |
33 ms |
16200 KB |
Output is correct |
4 |
Correct |
24 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15184 KB |
Output is correct |
2 |
Correct |
32 ms |
16208 KB |
Output is correct |
3 |
Correct |
35 ms |
18768 KB |
Output is correct |
4 |
Correct |
30 ms |
18512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
17992 KB |
Output is correct |
2 |
Correct |
58 ms |
16868 KB |
Output is correct |
3 |
Correct |
62 ms |
17232 KB |
Output is correct |
4 |
Correct |
74 ms |
16632 KB |
Output is correct |
5 |
Correct |
141 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
16968 KB |
Output is correct |
2 |
Correct |
69 ms |
21328 KB |
Output is correct |
3 |
Correct |
90 ms |
19544 KB |
Output is correct |
4 |
Correct |
63 ms |
22232 KB |
Output is correct |
5 |
Correct |
179 ms |
15944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
16476 KB |
Output is correct |
2 |
Correct |
72 ms |
17492 KB |
Output is correct |
3 |
Correct |
144 ms |
15928 KB |
Output is correct |
4 |
Correct |
93 ms |
16720 KB |
Output is correct |
5 |
Correct |
66 ms |
22352 KB |
Output is correct |
6 |
Correct |
236 ms |
15952 KB |
Output is correct |