Cod sursa(job #350229)

Utilizator ProtomanAndrei Purice Protoman Data 23 septembrie 2009 01:04:09
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <algorithm>
#include <iostream>
#include <fstream>

#define MAX 1000100
#define bazaHash 202387
#define ll long long

using namespace std;

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

inline void hashInsert(unsigned int el, int loc, int ha)
{
	for (elemHash *r = hash[ha][loc]; r; r = r->next)
		if (r->val == el)
		{
			r->ap++;

			return;
		}

	hashEl[ha]++;
	elemHash *r = new elemHash;
	r->val = el;
	r->ap = 1;
	r->next = hash[ha][loc];
	hash[ha][loc] = r;
}

inline void hashErase(unsigned int el, int loc, int ha)
{
	for (elemHash *r = hash[ha][loc], *prec = hash[ha][loc]; r; prec = r, r = r->next)
		if (r->val == el)
		{
			r->ap--;
			
			if (!r->ap)
			{
				hashEl[ha]--;

				if (r == hash[ha][loc])
					hash[ha][loc] = r->next;
				else prec->next = r->next;

				delete r;
			}

			return;
		}
}

inline int hashFind(unsigned int el, int loc, int ha)
{
	for (elemHash *r = hash[ha][loc]; r; r = r->next)
		if (r->val == el)
			return r->ap;

	return 0;
}

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

	cin >> n >> l >> u;

	hash[0][bazaHash] = new elemHash;
	hash[0][bazaHash]->val = 0;
	hash[1][bazaHash] = new elemHash;
	hash[1][bazaHash]->val = 0;

	for (unsigned int i = 1; i <= n; i++)
	{
		unsigned int x = 0;
		cin >> x;
		a[i] = x;
		loc[i] = x % bazaHash;

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

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

	cout << sol;

	return 0;
}