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 "shoes.h"
using namespace std;
vector<int>p[300005],a(800005);
void bld(int nd,int l,int r){
if(l==r)return a[nd]=1,void();
int md=(l+r)/2;
bld(nd*2,l,md);
bld(nd*2+1,md+1,r);
a[nd]=a[nd*2]+a[nd*2+1];
}
void res(int p,int nd,int l,int r){
if(l==r)return a[nd]=0,void();
int md=(l+r)/2;
if(p<=md)res(p,nd*2,l,md);
else res(p,nd*2+1,md+1,r);
a[nd]=a[nd*2]+a[nd*2+1];
}
int qry(int p,int q,int nd,int l,int r){
if(q<l||r<p)return 0;
if(p<=l&&r<=q)return a[nd];
int md=(l+r)/2;
return qry(p,q,nd*2,l,md)+qry(p,q,nd*2+1,md+1,r);
}
long long count_swaps(vector<int>s){
int n=s.size();
bld(1,0,n-1);
for(int i=n-1;0<=i;i--)p[s[i]+n].push_back(i);
long long r=0;
for(int i=0;i<n;i++){
if(0<p[s[i]+n].size()&&p[s[i]+n].back()!=i)continue;
res(p[s[i]+n].back(),1,0,n-1);
p[s[i]+n].pop_back();
r+=qry(i,p[n-s[i]].back(),1,0,n-1);
if(s[i]<0)r--;
res(p[n-s[i]].back(),1,0,n-1);
p[n-s[i]].pop_back();
}
return r;
}
# | 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... |