Submission #398612

#TimeUsernameProblemLanguageResultExecution timeMemory
398612richyrichArranging Shoes (IOI19_shoes)C++17
10 / 100
12 ms19100 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1e9+7;
const ll N = 2e5+10;
const ll inf = 1e9+100;
const double eps = 1;
int n, a[N], t[N*4];
set<int> L[N], R[N];
ll ans;
bool used[N];
void update(int tv, int tl, int tr, int l, int r) {
	if(l > r) return;
	if(tl == l && tr == r) {
		t[tv]++;
		return;
	}
	else {
		int tm =(tl + tr) >> 1;
		update(tv * 2, tl, tm, l, min(tm, r));
		update(tv * 2 + 1, tm + 1, tr, max(tm + 1, l), r);
	}
} 
ll get(int tv, int tl, int tr, int pos) {
	int tm = (tl+tr)>>1;
	if(tl == tr && tl == pos) return t[tv];
	else {
		if(pos <= tm) return t[tv] + get(tv * 2, tl, tm, pos);
		else return t[tv] + get(tv * 2 +1, tm + 1, tr, pos);
	}
}
long long count_swaps(std::vector<int> s) {
	/*for(int c : s) printf("%d ", c);
	printf("\n");*/
	ans = 0;
    n = s.size();
    for(int i = 1;i <= n;i++) {
    	a[i] = s[i-1];
    	if(a[i] < 0) L[-a[i]].insert(i);
    	else R[a[i]].insert(i);
    }
    /*for(int i = 1; i <= 2; i++) {
    	printf("left %d:", i);
    	for(int c : L[i]) printf("%d ", c);
    	printf("\n");
    }
    for(int i = 1; i <= 2; i++) {
    	printf("right %d:", i);
    	for(int c : R[i]) printf("%d ", c);
    	printf("\n");
    }*/
    for(ll i = 1; i <= n; i++){
    	if(used[i]) continue;
    	ll pos;
    	if(a[i] < 0) {
    		pos = *R[-a[i]].begin();
    		L[-a[i]].erase(i);
    		R[-a[i]].erase(pos);
    		ans += pos-(i+1) + get(1, 1, n, pos) - get(1, 1, n, i+1);
    		used[pos] = used[i] = 1;
    	}
    	else {
    		pos = *L[a[i]].begin();
    		R[a[i]].erase(i);
    		L[a[i]].erase(pos);
    		ans += pos - i + get(1, 1, n, pos) - get(1, 1, n, i);
    		used[pos] = used[i] = 1;
    	}
    	if(i+1 <= pos - 1)update(1, 1, n, i+1, pos - 1);
    }
    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...