# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
710748 | mseebacher | 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>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <functional>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define mp make_pair
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const lld pi = 3.14159265358979323846;
#define MAXI (int)2e5+5
// Entweder rechts oder unten -> dann nehmen oder nicht nehmen.
struct segtree{
int size;
vector<int> tree;
void init(int n){
int c = 1;
while(c < n) c*=2;
size = c;
tree.assign(size,0);
}
int query(int i,int x,int lx,int rx){
if(rx-lx==1) return tree[x];
int mid = (lx+rx)/2;
if(i<=mid) return tree[x]+query(i,2*x+1,lx,mid);
else return tree[x]+query(i,2*x+2,mid,rx);
}
void update(int val,int l,int r,int x,int lx,int rx){
if(l>r) return;
if(lx >= r || rx <= l) return;
if(lx >= l && rx <= r){
tree[x]+=val;
return;
}
int mid = (lx+rx)/2;
update(val,l,r,2*x+1,lx,mid);
update(val,l,r,2*x+2,mid,rx);
}
};
long long count_swaps(vector<int> a){
int n = s.size();
vector<set<int>> left(2*n+1);
vector<set<int>> right(2*n+1);
for(int i = 0;i<n;i++){
cin >> a[i];
if(a[i] < 0){
int x = a[i]*-1;
left[x].insert(i);
}else right[a[i]].insert(i);
}
segtree s; s.init(2*n);
vector<bool> marked(n,0);
ll ans = 0;
for(int i = 0;i<n;i++){
if(marked[i]) continue;
//Linker Schuh
if(a[i] < 0){
int x = a[i]*-1;
int indexRechts = *right[x].begin();
marked[indexRechts] = 1;
int wertRechts = indexRechts + s.query(indexRechts,0,0,s.size);
int wertLinks = i + s.query(i,0,0,s.size);
ans = ans + wertRechts- wertLinks - 1 ;
s.update(1,i,indexRechts-1,0,0,s.size);
right[x].erase(indexRechts);
left[x].erase(i);
}else{
int x = a[i];
int indexLinks = *left[x].begin();
marked[indexLinks] = 1;
int wertLinks = indexLinks + s.query(indexLinks,0,0,s.size);
int wertRechts = i + s.query(i,0,0,s.size);
ans = ans + wertLinks - wertRechts;
s.update(1,i,indexLinks-1,0,0,s.size);
left[x].erase(indexLinks);
right[x].erase(i);
}
//cout << ans << " ";
}
return ans;
}