This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
struct binary_indexed_tree{
int N;
vector<int> BIT;
binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
}
void add(int i, int x){
i++;
while (i <= N){
BIT[i] += x;
i += i & -i;
}
}
int sum(int i){
int ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
int sum(int L, int R){
return sum(R) - sum(L);
}
};
long long count_swaps(vector<int> S){
int n = S.size() / 2;
for (int i = 0; i < n * 2 - 1; i++){
assert(abs(S[i]) == abs(S[i + 1]));
}
int x = abs(S[0]);
map<int, queue<int>> Q;
for (int i = 0; i < n; i++){
Q[-x].push(i * 2);
Q[x].push(i * 2 + 1);
}
vector<int> p(n * 2);
for (int i = 0; i < n * 2; i++){
p[i] = Q[S[i]].front();
Q[S[i]].pop();
}
binary_indexed_tree BIT(n * 2);
long long ans = 0;
for (int i = 0; i < n * 2; i++){
ans += BIT.sum(p[i], n * 2);
BIT.add(p[i], 1);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |