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 all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
const int N=2e5+5;
long long t[N*4];
void build(int v,int tl,int tr){
if(tl==tr)t[v]=1;
else{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=t[v*2]+t[v*2+1];
}
}
void update(int v,int tl,int tr,int pos){
if(tl==tr)t[v]=0;
else{
int tm=(tl+tr)/2;
if(pos<=tm)update(v*2,tl,tm,pos);
else update(v*2+1,tm+1,tr,pos);
t[v]=t[v*2]+t[v*2+1];
}
}
long long get(int v,int tl,int tr,int l,int r){
if(r<tl or tr<l)return 0;
if(l<=tl && tr<=r)return t[v];
int tm=(tl+tr)/2;
return get(v*2,tl,tm,l,r)+get(v*2+1,tm+1,tr,l,r);
}
long long count_swaps(vector<int> s) {
int n=s.size();
s.insert(s.begin(),0);
build(1,1,n);
vector <int > v1[n+1],v0[n+1];
vector <int> used(n+1);
for(int i=1;i<=n;i++){
if(s[i]>0)v1[s[i]].pb(i);
if(s[i]<0)v0[-s[i]].pb(i);
}
int ans=0;
for(int i=1;i<=n;i++){
if(used[i])continue;
if(s[i]>0){
auto r=upper_bound(all(v0[s[i]]),i);
used[*r]=1;
update(1,1,n,i);
update(1,1,n,*r);
ans+=get(1,1,n,i,*r);
ans++;
}
else{
auto r=upper_bound(all(v1[-s[i]]),i);
used[*r]=1;
update(1,1,n,i);
update(1,1,n,*r);
ans+=get(1,1,n,i,*r);
}
}
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... |