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 mxn = 1e5+1 ;
vector<int> ft(mxn) ;
void update(int i ){
for(;i<mxn;i+=(i&-i)) ft[i]++ ;
}
int query(int i ){
int ans = 0 ;
for(;i>0;i-=(i&-i)) ans+=ft[i] ;
return ans ;
}
long long count_swaps(vector<int> a){
#define int long long
int n = a.size() ;
int ans = 0 ;
set<int> adj[n*3] ;
for(int i=0;i<n;i++) adj[a[i]+n].insert(i) ;
for(int i=0;i<n;i++){
if(a[i]==0) continue ;
// cout <<a[i] << " " ;
int oth = a[i]*-1 ;
oth+=n ;
a[i]+=n ;
adj[a[i]].erase(i) ;
int j = *(adj[oth].begin()) ;
adj[oth].erase(adj[oth].begin()) ;
int ze = query(j+1)-query(i+1) ;
ans+=j-i-(a[i]-n<0)-ze ;
a[i]=0 ;
a[j]= 0 ;
update(1+i) ;
update(j+1) ;
}
// cout << 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... |