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 MAXN = 1e5+1;
deque<int> d[2*MAXN];
bitset<2*MAXN> v;
int ft[2*MAXN];
int N;
void update(int k,int val){
int x=k+1;
while(x<=N){
ft[x]+=val;
x+=x&(-x);
}
}
int query(int k){
int x=k+1;
int s=0;
while(x>0){
s+=ft[x];
x-=x&(-x);
}
return s;
}
long long count_swaps(vector<int> s) {
N=s.size();
long long ans=0;
for(int i=0;i<N;i++)d[s[i]+MAXN].push_back(i);
for(int i=0;i<N;i++){
if(v[i])continue;
d[s[i]+MAXN].pop_front();
int h=d[MAXN-s[i]].front();
d[MAXN-s[i]].pop_front();
v[i]=1;
v[h]=1;
ans+=query(h)-query(i);
update(i,2);
update(h,-1);
if(s[i]>s[h])ans++;
}
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... |