#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class C>constexpr int len(const C&c){return int(c.size());}
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int MAXN = 200005;
int N; long long ans, bit[MAXN], wtf[MAXN];
void update(int pos, int val){
if(wtf[pos] == 1) assert(val == -1);
else assert(val == 1);
wtf[pos] += val;
for(int i = pos; i <= N; i += i & -i) bit[i] += val;
}
long long query(int pos){
long long tot = 0;
for(int i = pos; i > 0; i -= i & -i) tot += bit[i];
return tot;
}
vector<int> solve(){
int A; cin >> A;
if(A != 0){
update(A, 1);
return {A};
}
vector<int> left = solve(), right = solve();
if(len(left) > len(right)) swap(left, right);
sort(right.begin(), right.end());
long long cur, case1 = 0, case2 = 0;
for(auto i : left) update(i, -1);
for(auto i : left){
cur = lower_bound(right.begin(), right.end(), i) - right.begin();
case1 += cur;
case2 += len(right) - cur;
//assert(cur == (lower_bound(right.begin(), right.end(), i) - right.begin()));
}
ans += min(case1, case2);
for(auto i : left) update(i, 1), right.push_back(i);
return right;
}
int main(){
cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(bit, 0, sizeof(bit));
cin >> N; solve(); cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
1 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1920 KB |
Output is correct |
5 |
Correct |
2 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1920 KB |
Output is correct |
2 |
Correct |
2 ms |
1920 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1920 KB |
Output is correct |
2 |
Correct |
3 ms |
1920 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
6 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
2456 KB |
Output is correct |
2 |
Correct |
7 ms |
2048 KB |
Output is correct |
3 |
Correct |
305 ms |
2552 KB |
Output is correct |
4 |
Correct |
97 ms |
2544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
3916 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
3244 KB |
Output is correct |
2 |
Execution timed out |
1092 ms |
5500 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
14072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1046 ms |
3664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1089 ms |
9772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1089 ms |
6008 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
5548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |