# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
697282 | Baytoro | Arranging Shoes (IOI19_shoes) | C++17 | 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 <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl '\n'
#define ll long long
#define int long long
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const int INF=1e18;
int mod=1e9+7;
const int N=1e5+5;
int bit[N];
void update(int i, int v){
for(;i<=N;i+=i&-i){
bit[i]+=v;
}
}
int query(int i){
int res=0;
for(;i>0;i-=i&-i) res+=bit[i];
return res;
}
int query(int l, int r){
return query(r)-query(l-1);
}
vector<int> a[N][2];
int count_swaps(vector<int> s){
int n=s.size();
for(int i=1;i<=n;i++){
update(i,1);
a[abs(s[i-1])][s[i-1]<0].pb(i);
}
int res=0;
for(int i=1;i<=n;i++) for(int j=0;j<2;j++) reverse(all(a[i][j]));
for(int i=1;i<=n;i++){
if(!s[i-1]) continue;
int x=s[i-1],y=(s[i-1]<0),z=a[abs(x)][y^1].back();
a[abs(x)][0].pop_back();
a[abs(x)][1].pop_back();
s[z-1]=0;
s[i-1]=0;
update(z,-1);
if(y) res+=query(i+1,z);
else res+=query(i,z);
}
return res;
}