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 MAX_N=1e5+9;
int a[8*MAX_N];
void build(int l,int r, int p){
if(l==r){
a[p]=l;
return;
}
build(l,(l+r)/2,p*2);
build((l+r)/2+1,r,p*2+1);
a[p]=0;
return;
}
int find(int value,int l, int r, int p){
if(l==r){
if(l==value){
return a[p];
}else{
return 0;
}
}
a[p*2]+=a[p];
a[p*2+1]+=a[p];
a[p]=0;
if(l>value||value>r){
return 0;
}
return find(value,(r+l)/2+1,r,p*2+1)+find(value,l,(r+l)/2,p*2);
}
void update(int x,int y,int l,int r,int p){
if(l==r){
if( r<=y && r>=x )
a[p]++;
return;
}
a[p*2]+=a[p];
a[p*2+1]+=a[p];
a[p]=0;
if(r<x||l>y)
return;
if(r<=y&&l>=x){
a[p*2]++;
a[p*2+1]++;
return;
}
update(x,y,l,(l+r)/2,p*2);
update(x,y,(l+r)/2+1,r,p*2+1);
return;
}
long long count_swaps(std::vector<int> S){
int n=S.size()/2;
long long int ans=0;
build(0,n*2-1,1);
vector<vector<int> > h(n*2);
for(int i=0;i<n*2;i++){
if(h[S[i]*(-1)+n].size()==0){
h[(S[i]+n)].push_back(i);
}else{
int x=find(h[S[i]*(-1)+n][0],0,2*n-1,1);
int y=find(i,0,2*n-1,1);
if(S[i]<0)
ans++;
ans+=y-x;
ans--;
update(h[S[i]*(-1)+n][0],i,0,2*n-1,1);
h[S[i]*(-1)+n].erase(h[S[i]*(-1)+n].begin());
}
}
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... |