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 <bits/stdc++.h>
using namespace std;
int seg[800005],lz[800005],a[200005],alr[200005];
vector <int> v[200005];
void build(int id,int x,int y){
seg[id]=0; lz[id]=0;
if (x==y) return;
int mid=(x+y)/2;
build(id*2,x,mid);
build(id*2+1,mid+1,y);
}
void update(int id,int x,int y,int l,int r){
if (r<x || y<l) return;
if (l<=x && y<=r){
seg[id]+=(y-x+1);
lz[id]++;
return;
}
int mid=(x+y)/2;
if (lz[id]>0){
seg[id*2]+=(mid-x+1)*lz[id];
seg[id*2+1]+=(y-mid)*lz[id];
lz[id*2]+=lz[id];
lz[id*2+1]+=lz[id];
lz[id]=0;
}
update(id*2,x,mid,l,r);
update(id*2+1,mid+1,y,l,r);
seg[id]=seg[id*2]+seg[id*2+1];
}
int query(int id,int x,int y,int l,int r){
if (r<x || y<l) return 0;
if (l<=x && y<=r) return seg[id];
int mid=(x+y)/2;
if (lz[id]>0){
seg[id*2]+=(mid-x+1)*lz[id];
seg[id*2+1]+=(y-mid)*lz[id];
lz[id*2]+=lz[id];
lz[id*2+1]+=lz[id];
lz[id]=0;
}
return (query(id*2,x,mid,l,r)+query(id*2+1,mid+1,y,l,r));
}
long long count_swaps(std::vector<int> s) {
long long ans=0;
int n=s.size()/2;
build(1,1,2*n);
for (int i=2*n-1;i>=0;i--){
if (s[i]<0) v[-s[i]].push_back(i+1);
else v[s[i]+n].push_back(i+1);
alr[i+1]=0;
}
for (int i=0;i<2*n;i++){
if (alr[i+1]) continue;
int ptl=query(1,1,2*n,i+1,i+1)+i+1;
alr[i+1]=1;
if (s[i]<0){
int ptr=query(1,1,2*n,v[-s[i]+n].back(),v[-s[i]+n].back());
ptr+=v[-s[i]+n].back();
alr[v[-s[i]+n].back()]=1;
if (ptr==ptl+1){
v[-s[i]+n].pop_back();
v[-s[i]].pop_back();
continue;
}
//cout<<s[i]<<" "<<ptl<<" "<<ptr<<" "<<ptr-ptl<<"\n";
ans+=(ptr-ptl-1);
update(1,1,2*n,i+1,v[-s[i]+n].back()-1);
v[-s[i]+n].pop_back();
v[-s[i]].pop_back();
}
else{
int ptr=query(1,1,2*n,v[s[i]].back(),v[s[i]].back());
ptr+=v[s[i]].back();
//cout<<s[i]<<" "<<ptl<<" "<<ptr<<" "<<ptr-ptl<<"\n";
alr[v[s[i]].back()]=1;
ans+=(ptr-ptl);
update(1,1,2*n,i+2,v[s[i]].back()-1);
v[s[i]].pop_back();
v[s[i]+n].pop_back();
}
//cout<<ans<<"\n";
}
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... |