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 F first
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) lli(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
lli segt[8*100000]={};
void up(lli node,lli l,lli r,lli in)
{
if(l==r)
{
segt[node]=1;
return;
}
lli m=(l+r)/2;
if(in<=m)
up(2*node+1,l,m,in);
else
up(2*node+2,m+1,r,in);
segt[node]=segt[2*node+1]+segt[2*node+2];
}
lli qu(lli node,lli l,lli r,lli s,lli e)
{
if(l>=s&&r<=e)
return(segt[node]);
if(l>e||r<s)
return(0);
lli m=(l+r)/2;
lli q1=qu(2*node+1,l,m,s,e),q2=qu(2*node+2,m+1,r,s,e);
return(q1+q2);
}
long long count_swaps(std::vector<int> s)
{
lli n=s.size(),f;
lli ans=0;
set<ii>inva,vain;
for(lli i=0;i<n;++i)
{
f=s[i];
if(f<0)
f=abs(f)+n/2;
inva.insert(mp(i,f));
vain.insert(mp(f,i));
}
while(inva.size())
{
ii it,f=(*inva.begin());
inva.erase(inva.begin());
vain.erase(mp(f.second,f.F));
f.F+=qu(0,0,n-1,f.F,n-1);
if(f.second>n/2)
it=(*vain.lower_bound(mp(f.second-n/2,0)));
else
it=(*vain.lower_bound(mp(f.second+n/2,0)));
vain.erase(it);
inva.erase(mp(it.second,it.F));
up(0,0,n-1,it.second);
it.second+=qu(0,0,n-1,it.second,n-1)-1;
ans+=it.second-f.F-1;
if(f.second<=n/2)
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... |