Cod sursa(job #351028)

Utilizator ProtomanAndrei Purice Protoman Data 26 septembrie 2009 16:39:46
Problema Secventa 5 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <algorithm>
#include <iostream>
#include <fstream>

#define MAX 1000100
#define bazaHash 666013
#define ll long long
#define ui unsigned int

using namespace std;

class elemHash
{
public:
	ui val;
	int ap;
} hash[2][bazaHash + 1][7];
ui hashEl[2];
ui n, l, u, stLow= 1, stUp = 1;
ll sol;
ui a[MAX];

inline void hashInsert(ui el, ui loc, int ha)
{
	for (int i = 1; i <= hash[ha][loc][0].val; i++)
		if (hash[ha][loc][i].val == el)
		{
			hash[ha][loc][i].ap++;

			return;
		}

	hashEl[ha]++;
	hash[ha][loc][0].val++;
	hash[ha][loc][hash[ha][loc][0].val].val = el;
	hash[ha][loc][hash[ha][loc][0].val].ap = 1;
}

inline void hashErase(ui el, ui loc, int ha)
{
	for (int i = 1; i <= hash[ha][loc][0].val; i++)
		if (hash[ha][loc][i].val == el)
		{
			hash[ha][loc][i].ap--;
			
			if (!hash[ha][loc][i].ap)
			{
				hashEl[ha]--;

				swap(hash[ha][loc][i], hash[ha][loc][hash[ha][loc][0].val]);
				hash[ha][loc][0].val--;
			}

			return;
		}
}

int main()
{
	ifstream cin("secv5.in");
	ofstream cout("secv5.out");

	cin >> n >> l >> u;

	for (ui i = 1; i <= n; i++)
	{
		ui x = 0;
		cin >> x;
		a[i] = x;

		for (hashInsert(x, x % bazaHash, 0); hashEl[0] == l; hashErase(a[stLow], a[stLow] % bazaHash, 0), stLow++);
			
		for (hashInsert(x, x % bazaHash, 1); hashEl[1] > u; hashErase(a[stUp], a[stUp] % bazaHash, 1), stUp++);

		if (hashEl[0] == l - 1)
			sol += (ll) (stLow - stUp);
	}

	cout << sol;

	return 0;
}