Cod sursa(job #350190)

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

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

using namespace std;

int n, l, u, stLow= 1, stUp = 1;
ll sol;
unsigned int a[MAX];
vector <pair <unsigned int, int> > hashLow[bazaHash + 1], hashUp[bazaHash + 1];

inline void hashInsert(vector <pair <unsigned int, int> > hash[], unsigned int el)
{
	unsigned int loc = el % bazaHash;

	for (int i = 0; i < hash[loc].size(); i++)
		if (hash[loc][i].f == el)
		{
			hash[loc][i].s++;

			return;
		}

	hash[bazaHash][0].f++;
	hash[loc].pb(mp(el, 1));
}

inline void hashErase(vector <pair <unsigned int, int> > hash[], unsigned int el)
{
	unsigned int loc = el % bazaHash;

	for (int i = 0; i < hash[loc].size(); i++)
		if (hash[loc][i].f == el)
		{
			hash[loc][i].s--;
			
			if (!hash[loc][i].s)
			{
				hash[bazaHash][0].f--;

				swap(hash[loc][i], hash[loc][hash[loc].size() - 1]);
				hash[loc].pop_back();
			}

			return;
		}
}

inline int hashFind(vector <pair <unsigned int, int> > hash[], unsigned int el)
{
	unsigned int loc = el % bazaHash;

	for (int i = 0;  i< hash[loc].size(); i++)
		if (hash[loc][i].f == el)
			return hash[loc][i].s;

	return 0;
}

int main()
{
	freopen("secv5.in", "r", stdin);
	freopen("secv5.out", "w", stdout);

	scanf("%d %d %d\n", &n, &l, &u);

	hashLow[bazaHash].pb(mp(0, 0));
	hashUp[bazaHash].pb(mp(0, 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;

		hashInsert(hashLow, x);
		for (; hashLow[bazaHash][0].f > l; hashErase(hashLow, a[stLow]), stLow++);
		for (; hashFind(hashLow, a[stLow]) > 1; hashErase(hashLow, a[stLow]), stLow++);
			
		hashInsert(hashUp, x);
		for (; hashUp[bazaHash][0].f > u; hashErase(hashUp, a[stUp]), stUp++);

		if (hashLow[bazaHash][0].f >= l)
			sol += (ll) (stLow - stUp) + 1;
	}

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

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