Cod sursa(job #350209)

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

#define MAX 1000100
#define bazaHash 666013
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

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

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

			return;
		}

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

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

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

				delete r;
			}

			return;
		}
}

inline int hashFind(elemHash* hash[], unsigned int el, int loc)
{
	for (elemHash *r = hash[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);

	hashLow[bazaHash] = new elemHash;
	hashLow[bazaHash]->val = 0;
	hashUp[bazaHash] = new elemHash;
	hashUp[bazaHash]->val = 0;

	for (int i = 1; i <= n; i++)
	{
		unsigned int x = 0;
		char nrmX[16];
		fgets(nrmX + 1, 16, stdin);
		int lung = strlen(nrmX + 1);

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

		hashInsert(hashLow, x, loc[i]);
		for (; hashLow[bazaHash]->val > (unsigned int) l; hashErase(hashLow, a[stLow], loc[stLow]), stLow++);
		for (; hashFind(hashLow, a[stLow], loc[stLow]) > 1; hashErase(hashLow, a[stLow], loc[stLow]), stLow++);
			
		hashInsert(hashUp, x, loc[i]);
		for (; hashUp[bazaHash]->val > (unsigned int) u; hashErase(hashUp, a[stUp], loc[stUp]), stUp++);

		if (hashLow[bazaHash]->val >= (unsigned int) l)
			sol += (ll) (stLow - stUp) + 1;
	}

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

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