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;
using i64 = long long;
i64 count_swaps(vector<int> arr){
int n = arr.size();
map<int,queue<int>> mp;
vector<int> bit(n + 1);
i64 ans = 0;
for(int i = 0;i<n;i++){
int e = arr[i];
int ind;
if(mp[-e].empty()){
ind = i + 1;
mp[e].push(ind);
}else{
ind = mp[-e].front();
mp[-e].pop();
ans += i;
for(int j = ind;j > 0;j -= (j & -j)){
ans -= bit[j];
}
if(e < 0) ans ++;
}
// adding twice if found both the pair
for(;ind <= n;ind += (ind & -ind)){
bit[ind] ++;
}
}
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... |