# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1193376 | yoshi | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include<bits/stdc++.h>
#include "shoes.h"
#define ll long long
#define pb push_back
#define ss second
#define ff first
using namespace std;
ll df[1234567],ans;
vector<ll> vv[1234567];
struct segtree{
ll n;
vector<ll>d;
segtree(ll n){
d.resize(4*n);
build(1,1,n);
}
void build(ll v, ll l, ll r){
if(l == r){
d[v] = 1;
return;
}
ll m = (l+r)/2;
build(v*2,l,m);
build(v*2+1,m+1,r);
d[v] = d[v*2]+d[v*2+1];
}
void update(ll v,ll l, ll r, ll pos, ll val){
if(pos < l or pos > r)return;
if(l == r){
d[v] = val;
return;
}
ll m = (l+r)/2;
update(v*2,l,m,pos,val);
update(v*2+1,m+1,r,pos,val);
d[v] = d[v*2]+d[v*2+1];
}
ll query(ll v, ll l, ll r, ll L, ll R){
if(R<L||R<l||r<L) return 0ll;
if(L<=l&&r<=R) return d[v];
ll m = (l+r)/2;
return query(v*2,l,m,L,R) + query(v*2+1,m+1,r,L,R);
}
};
int count_swap(vector<int> a){
int n=a.size()/2;
vector<ll> s;
s.pb(0);
for(auto x: a) s.pb(x);
map<ll,vector<ll>> w;
for(ll i=1; i<=s.size(); i++){
w[s[i]].pb(i);
}
segtree yo(2*n);
ll c=0;
ll cnt=0;
vector<pair<ll,ll>> v;
for(ll i=1 ; i<=100000; i++){
for(ll j=0; j<w[i].size(); j++){
if(w[-i][j] < w[i][j]){
v[c].ff=w[-i][j]+1;
v[c].ss=w[i][j]-1;
}else{
v[c].ff=w[-i][j]-1;
v[c].ss=w[i][j]+1;
cnt++;
}
c++;
}
}
sort(v.begin(), v.end());
for(ll i=0;i<c; i++){
cnt+= yo.query(1 , 1 ,2*n , v[i].ff, v[i].ss);
yo.update(1, 1, 2*n, v[i].ss+1 , 0);
}
return cnt;
}