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>
#include "shoes.h"
using namespace std;
#define rep(i,n) for(int i=1; i<=n; i++)
#define pb push_back
int fw[200010],n;
long long ans;
deque<int> nex[200010];
void upd(int x, int v){
for(; x<200005; x+=x&-x)fw[x]+=v;
}
int sum(int x){
int ret=0;
for(; x; x-=x&-x)ret+=fw[x];
return ret;
}
long long count_swaps(vector<int> s) {
n=s.size();
rep(i,n)nex[100000+s[i-1]].pb(i);
rep(i,n)upd(i,1);
rep(i,n){
int x=s[i-1];
if(sum(i)-sum(i-1)==0)continue;
if(x>0)ans++;
nex[100000+x].pop_front();
int j=nex[100000-x][0];
nex[100000-x].pop_front();
//cout<<j<<endl;
ans+=sum(j)-sum(i)-1;
upd(i,-1);
upd(j,-1);
//cout<<sum(j)-sum(j-1)<<endl;
//cout<<ans<<endl;
}
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... |