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+10;
deque<int> d[2*MAXN];
bitset<2*MAXN> v;
long long ft[2*MAXN];
int N;
void update(int k,long long val){
int x=k+1;
while(x<=N){
ft[x]+=val;
x+=x&(-x);
}
}
long long query(int k){
int x=k+1;
long long 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++)update(i,1);
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+1);
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... |