# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
143174 | mat_v | 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 <bits/stdc++.h>
#include <cstdio>
#include <cassert>
#define pb push_back
using namespace std;
int n;
vector<int> tike[100005][2];
int dokle[100005][2];
int seg[200005];
int lazy[200005];
bool bio[200005];
void init(int l, int r, int ind){
if(l == r){
seg[ind] = l;
return;
}
int mid = (l + r) / 2;
init(l, mid, ind * 2);
init(mid + 1, r, ind * 2 + 1);
seg[ind] = seg[ind * 2] + seg[ind * 2 + 1];
}
void propagate(int l, int r, int ind){
if(lazy[ind] != 0){
seg[ind] -= (r - l + 1)*lazy[ind];
int mid = (l + r) / 2;
if(l != r){
lazy[ind * 2] = lazy[ind];
lazy[ind * 2 + 1] = lazy[ind];
}
lazy[ind] = 0;
}
}
int query(int l,int r, int ind, int gde){
propagate(l,r,ind);
if(l == r)return seg[ind];
int mid = (l + r)/2;
if(gde <= mid)return query(l,mid,ind * 2, gde);
else return query(mid + 1, r, ind * 2 + 1, gde);
}
void update(int l, int r, int ind, int levo, int desno){
propagate(l,r,ind);
if(levo >= l && desno <= r){
lazy[ind]++;
propagate(l,r,ind);
return;
}
if(desno < l || r < levo)return;
int mid = (l + r) / 2;
update(l,mid,ind * 2,levo,desno);
update(mid + 1,r,ind * 2 + 1,levo,desno);
propagate(l,mid,ind*2);
propagate(mid+1,r,ind*2+1);
seg[ind] = seg[ind*2] + seg[ind*2+1];
propagate(l,r,ind);
}
long long count_swaps(std::vector<int> s) {
n = s.size()/2;
for(int i=0; i<2*n; i++){
if(s[i] < 0)tike[abs(s[i])][0].pb(i);
else tike[s[i]][1].pb(i);
}
int l = 0;
int shift = 0;
init(0,2*n-1,1);
long long res = 0;
while(l < 2*n){
if(bio[l]){
l++;
continue;
}
int koji = abs(s[l]);
int ind;
if(s[l] < 0)ind = 1;
else ind = 0;
int gde = tike[koji][ind][dokle[koji][ind]];
bio[gde] = 1;
dokle[koji][ind]++;
dokle[koji][ind^1]++;
int pom = query(0,2*n-1,1,gde);
//cout << pom << " " << gde << endl;
if(gde != 2*n-1){
// cout << "kurac";
update(0,2*n-1,1,gde+1,2*n-1);
}
//bio[l + 1] = 1;
res += (pom - l - 1);
res += 1-ind;
//cout << l << " " << pom << endl;
//cout << l << " " << res << endl;
l ++;
}
return res;
}
int main() {
//int n;
cin >> n;
vector<int> S(2 * n);
for (int i = 0; i < 2 * n; i++)
cin >> S[i];
long long result = count_swaps(S);
cout << result;
return 0;
}
/*
2
-1 -2 1 2
*/