Submission #259414

#TimeUsernameProblemLanguageResultExecution timeMemory
259414astoriaArranging Shoes (IOI19_shoes)C++14
100 / 100
241 ms207096 KiB
#include "shoes.h"
#include "bits/stdc++.h"
using namespace std;

#define ll long long

#define MAXN 200005
int ft[MAXN];

int sum(int r){
	int an=0;
	for(int i=r; i>=0; i=(i&(i+1))-1){
		an += ft[i];
	}
	return an;
}

int sum(int l, int r){
	if(l!=0) return sum(r)-sum(l-1);
	else return sum(r);
}

void add(int r, int x){
	for(int i=r; i<MAXN; i|=(i+1)){
		ft[i] += x;
	}
}

long long count_swaps(std::vector<int> s) {
	int n=s.size()/2;
	int clo[2*n];
	queue<int> have[3][n+5];
	for(int i=0; i<2*n; i++){
		int x = s[i], y=abs(s[i]);
		if(x<0){
			if(!have[2][y].empty()){
				clo[have[2][y].front()] = i;
				have[2][y].pop();
			}
			else have[1][y].push(i);
		}
		if(x>0){
			if(!have[1][y].empty()){
				clo[have[1][y].front()] = i;
				have[1][y].pop();
			}
			else have[2][y].push(i);
		}
	}
	memset(ft,0,sizeof(ft));
	bool taken[2*n];
	memset(taken,0,sizeof(taken));
	ll an=0;
	for(int i=0; i<2*n; i++){
		if(taken[i]) continue;
		int b = sum(i,clo[i]);
		b += (clo[i]-i-1);
		if(s[i]>0) b++;
		an+=b;
		add(clo[i],-1);
		taken[clo[i]]=1;
	}
	return an;
}
#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...