Submission #826897

#TimeUsernameProblemLanguageResultExecution timeMemory
826897vjudge1Arranging Shoes (IOI19_shoes)C++17
10 / 100
84 ms9912 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]<<' ';

int tt[10];

ll t[1000100], b[1001000], a[400100];

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);
		

}

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]] = i;
		else pos1[-a[i]] = 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';
			ans += get(1, 1, n, 1, pos1[a[i]])-1;
			b[i] = 0;
			b[pos1[a[i]]] = 0;
			upd(1, 1, n, i, 0);
			upd(1, 1, n, pos1[a[i]], 0);
		}
		else{
			b[i] = 0;
			upd(1, 1, n, i, 0);
			ans += get(1, 1, n, 1, pos0[-a[i]])-1;
			b[pos0[-a[i]]] = 0; 
			upd(1, 1, n, pos0[-a[i]], 0);
		}
		
	}

		
	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...