Cod sursa(job #350222)

Utilizator ProtomanAndrei Purice Protoman Data 23 septembrie 2009 00:54:28
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <algorithm>
#include <stdio.h>

#define MAX 1000100
#define bazaHash 234567
#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()
{
	freopen("secv5.in", "r", stdin);
	freopen("secv5.out", "w", stdout);

	scanf("%d %d %d\n", &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;
		char nrmX[16];
		fgets(nrmX , 16, stdin);
		int lung = strlen(nrmX);

		for (int j = 0; j < lung - 1; j++)
			x = x * 10 + nrmX[j] - '0';
		a[i] = x;
		loc[i] = x % bazaHash;

		hashInsert(x, loc[i], 0);
		for (; hashEl[0] > l; hashErase(a[stLow], loc[stLow], 0), stLow++);
		for (; hashFind(a[stLow], loc[stLow], 0) > 1; 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)
			sol += (ll) (stLow - stUp) + 1;
	}

	printf("%lld\n", sol);

	fclose(stdin);
	fclose(stdout);
	return 0;
}