Submission #200382

#TimeUsernameProblemLanguageResultExecution timeMemory
200382luciocfArranging Shoes (IOI19_shoes)C++14
100 / 100
193 ms33016 KiB
#include <bits/stdc++.h>
#include "shoes.h"

using namespace std;

typedef long long ll;

const int maxn = 2e5+10;

int a[maxn];

set<int> pos_esq[maxn], pos_dir[maxn];

int bit[maxn];

void upd(int x, int v)
{
	for (; x < maxn; x += (x&-x))
		bit[x] += v;
}

int soma(int x)
{
	int ans = 0;
	for (; x > 0; x -= (x&-x))
		ans += bit[x];
	return ans;
}

ll count_swaps(vector<int> s)
{
	int n = (int)s.size();

	for (int i = 1; i <= n; i++)
	{
		a[i] = s[i-1];
		upd(i, 1);
	}

	for (int i = 1; i <= n; i++)
	{
		if (a[i] < 0) pos_esq[-a[i]].insert(i);
		else pos_dir[a[i]].insert(i);
	}

	ll ans = 0;

	for (int i = 1; i <= n; i++)
	{
		if (a[i] < 0)
		{
			a[i] = -a[i];

			if (pos_esq[a[i]].find(i) == pos_esq[a[i]].end())
				continue;

			int dir = *(pos_dir[a[i]].upper_bound(i));

			ans += 1ll*(soma(dir)-soma(i)-1);

			pos_esq[a[i]].erase(i); pos_dir[a[i]].erase(dir);

			upd(dir, -1); upd(i, -1);
		}
		else
		{
			if (pos_dir[a[i]].find(i) == pos_dir[a[i]].end())
				continue;

			int esq = *(pos_esq[a[i]].upper_bound(i));

			ans += 1ll*(soma(esq)-soma(i));

			pos_dir[a[i]].erase(i); pos_esq[a[i]].erase(esq);

			upd(i, -1); upd(esq, -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...