Submission #826908

#TimeUsernameProblemLanguageResultExecution timeMemory
826908vjudge1Arranging Shoes (IOI19_shoes)C++17
100 / 100
363 ms548848 KiB
#include<bits/stdc++.h>
#include "shoes.h"

using namespace std;

#define all(x) x.begin(), x.end()
#define pb push_back
#define ll long long
#define vout(v) for(int i=0; i<v.size(); i++) cout<<v[i]<<' ';
            
ll t[2200100], b[2201000], a[800100];

void build(int v, int l, int r){
	if(l == r){
		t[v] = 1;
		return;
	}
	int mid=(l+r)/2;
	build(v+v, l, mid);
	build(v+v+1, mid+1, r);
	t[v] = t[v+v]+t[v+v+1];
}

void upd(int v, int l, int r, int pos, int x){
	if(l == r){
		t[v] = x;
		return;
	}
	int mid=(l+r)/2;
	if(mid>=pos){
		upd(v+v, l, mid, pos, x);
	}
	else{
		upd(v+v+1, mid+1, r, pos, x);
	}
	t[v] = t[v+v]+t[v+v+1];
}

ll get(int v, int l, int r, int L, int R){
	if(r < L or R < l){
		return 0;
	}
	if(L <= l and r <= R){
		return t[v];
	}
	int mid=(l+r)/2;
	return get(v+v, l, mid, L, R) + get(v+v+1, mid+1, r, L, R);
		

}

deque<int> pos1[400100], pos0[400100];


 
long long count_swaps(vector<int> S){
	int n = S.size();
	for(int i=1; i<=n; i++){
		a[i] = S[i-1];
	}
	for(int i=1; i<=n; i++){
		if(a[i] > 0) pos0[a[i]].pb(i);
		else pos1[-a[i]].pb(i);
		b[i] = 1;
	}

	ll ans=0;
	build(1, 1, n);
	for(int i=1; i<=n; i++){   
		if(b[i] == 0){
			continue;
		}
		if(a[i] > 0){
			//cout<<i<<' '<<a[i]<<' '<<pos1[a[i]]<<'\n';
			int x = pos1[a[i]].front();
			ans += get(1, 1, n, 1, x)-1;
			b[i] = 0;
			b[x] = 0;
			upd(1, 1, n, i, 0);
			upd(1, 1, n, x, 0);
			pos0[a[i]].pop_front();
			pos1[a[i]].pop_front();
			
		}
		else{
			//cout<<i<<' '<<a[i]<<' '<<pos0[-a[i]]<<'\n';
			b[i] = 0;
			upd(1, 1, n, i, 0);
			int x = pos0[-a[i]].front();
			ans += get(1, 1, n, 1, x)-1;
			b[x] = 0; 
			upd(1, 1, n, x, 0);
			pos0[-a[i]].pop_front();
			pos1[-a[i]].pop_front();
			
		}
		
	}

		
	return ans;
}












#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...