# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
155981 | giorgikob | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
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"
#include "grader.h"
int n,ans,r,pos;
vector<int>locl[500001],locr[500001],indl[500001],indr[500001];
int tree[500001];
void upd(int node,int tl,int tr){
if(tl==tr){
tree[node] = 1;
return;
}
int mid = (tl+tr)/2;
if(pos<mid)
upd(node*2,tl,mid);
else
upd(node*2+1,mid+1,tr);
tree[node] = tree[node*2] + tree[node*2+1];
}
int get(int node,int tl,int tr){
if(r<tl)return 0;
if(tr<=tr)return tree[node];
int mid = (tr+tl)/2;
int x = get(node*2,tl,mid);
int y = get(node*2+1,mid+1,tr);
return x+y;
}
long long count_swaps(std::vector<int> s) {
n = s.size()/2;
for(int i=0;i<s.size();i++){
if(s[i]<0){
locl[-s[i]] = i;
} else {
locr[s[i]] = i;
}
}
for(int i=0;i<s.size();i++){
if(s[i]<0){
int loc = locr[ s[i] ][ indr[ s[i] ] ];
r = loc;
ans += loc - i - get(1,0,s.size()-1);
indr[ s[i] ] ++;
pos = loc;
upd(1,0,s.size()-1);
} else {
int loc = locl[ s[i] ][ indl[ s[i] ] ];
r = loc;
ans += loc - i + 1 - get(1,0,s.size()-1);
indl[ s[i] ] ++;
pos = loc;
upd(1,0,s.size()-1);
}
}
return ans;
}