#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
int n;
struct BIT {
vector<int> tree;
vector<int> elems;
BIT() {
tree = vector<int>(N,0);
}
void add(int v) {
elems.push_back(v);
for (; v < n; v|=v+1)
tree[v]++;
}
int sum(int v) {
int ans = 0;
for (; v >= 0; v = (v&(v+1))-1)
ans += tree[v];
return ans;
}
int merge(BIT &other) {
int ans = 0;
for (int x : other.elems)
ans += sum(x);
for (int x : other.elems)
add(x);
return ans;
}
};
int merge(BIT &a, BIT &b) {
if (a.elems.size() < b.elems.size())
swap(a,b);
int y = a.elems.size() * b.elems.size();
int x = a.merge(b);
return min(x, y-x);
}
BIT solve(int &ans) {
int x; cin>>x;
if (x) {
BIT ds;
ds.add(x-1);
return ds;
} else {
BIT a = solve(ans), b = solve(ans);
ans += merge(a,b);
return a.elems.size()>b.elems.size() ? a : b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int ans = 0;
solve(ans);
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5872 KB |
Output is correct |
2 |
Correct |
13 ms |
6688 KB |
Output is correct |
3 |
Correct |
14 ms |
5824 KB |
Output is correct |
4 |
Correct |
12 ms |
4984 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
7420 KB |
Output is correct |
2 |
Correct |
66 ms |
24584 KB |
Output is correct |
3 |
Correct |
66 ms |
10476 KB |
Output is correct |
4 |
Correct |
63 ms |
19980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
19064 KB |
Output is correct |
2 |
Correct |
464 ms |
18512 KB |
Output is correct |
3 |
Correct |
425 ms |
10584 KB |
Output is correct |
4 |
Runtime error |
68 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
276 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
73 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1065 ms |
49888 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
92 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
63 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1064 ms |
10612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
66 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
73 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |