Submission #487153

#TimeUsernameProblemLanguageResultExecution timeMemory
487153M_WArranging Shoes (IOI19_shoes)C++14
10 / 100
1103 ms134944 KiB
#include <bits/stdc++.h>
using namespace std;
queue<int> q[100001], q2[100001];
bool mark[200001];

int fwt[200001];

void ins(int v, int a){
	for(; v <= 200000; v += (v & -v)) fwt[v] += a;
}

int query(int v){
	int ret = 0;
	for(; v > 0; v -= (v & -v)) ret += fwt[v];
	return ret; 
}

long long count_swaps(vector<int> S){
	int len = S.size();
	for(int i = 0; i < len; i++){
		if(S[i] > 0) q[S[i]].push(i);
		else q2[-S[i]].push(i);
	}
	long long ans = 0;
	for(int i = 0; i < len; i++){
		if(mark[i]) continue;
		mark[i] = true;
		if(S[i] < 0){
			q2[-S[i]].pop();
			int pos = q[-S[i]].front() + query(q[-S[i]].front());
			int pos2 = i + query(i);
			ans += (pos - pos2 - 1) * 1ll;
			if(pos - pos2 - 1 != 0){
				ins(pos2, 1);
				ins(pos + 1, -1);
			} 
			mark[q[-S[i]].front()] = true;
			q[-S[i]].pop();
		}
		else{
			q[S[i]].pop();
			int pos = q2[S[i]].front() + query(q2[S[i]].front());
			int pos2 = i + query(i);
			ans += (pos - pos2) * 1ll;
			ins(pos2 + 1, 1);
			ins(pos + 1, -1);
			mark[q2[S[i]].front()] = true;
			q2[S[i]].pop();
		}
	}
	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...