#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;
// and so on
struct bit {
int* tree;
int siz;
vector<int> v;
void init(int x) {
for (siz = 1; siz < x; siz <<= 1)
;
tree = new int[siz + 1];
for (int i = 1; i <= siz; i++) tree[i] = 0;
}
void add(int pos, int x) {
v.push_back(pos);
while (pos <= siz) {
tree[pos] += x;
pos += (pos & -pos);
}
}
int sum(int pos) {
int ret = 0;
if(pos<=0) return 0;
while (pos) {
ret += tree[pos];
pos &= (pos - 1);
}
return ret;
}
};
bit st[200001];
int id = 0;
pll sol() {
ll c, ret = 0;
cin >> c;
if (c == 0) {
auto [sl, a] = sol();
auto [sr, b] = sol();
ret += sl + sr;
if(st[a].v.size() < st[b].v.size()) swap(a, b);
ll acnt = 0, bcnt = 0;
int siz = st[a].v.size();
for(auto x : st[b].v){
int cnt = st[a].sum(x-1);
acnt += cnt;
bcnt += siz - cnt;
}
ret += min(acnt, bcnt);
for(auto x : st[b].v){
st[a].add(x, 1);
}
return {ret, a};
} else {
int idx = id++;
st[idx].init(200000);
st[idx].add(c, 1);
return {0, idx};
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
auto [ans, idx] = sol();
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28756 KB |
Output is correct |
2 |
Correct |
13 ms |
27700 KB |
Output is correct |
3 |
Correct |
13 ms |
28756 KB |
Output is correct |
4 |
Correct |
12 ms |
28756 KB |
Output is correct |
5 |
Correct |
5 ms |
10196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
30 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
31 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
35 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
37 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |