Cod sursa(job #10922)

Utilizator ProstuStefan-Alexandru Filip Prostu Data 29 ianuarie 2007 22:10:24
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
/*
 * Cel putin L - cel putin U + 1 :)
 * this way seems to be a little more simple
 *
 * HASH is BAD, HASH is EVIL
 */

#include <cstdio>
#include <cmath>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

const int NMAX = 1 << 20;

#define MP make_pair

int N, L, U, W;
int A[NMAX], V[NMAX];
hash_map <int, int> H;

void read() {
	FILE *fin = fopen("secv5.in", "rt");
	int i, j, aux, t;
	char buf[16];

	fscanf(fin, " %d %d %d ", &N, &L, &U);

	for (i = 0; i < N; ++i) {
//		fgets(buf, 16, fin);

//		for (aux = 0, j = 0; buf[j] && buf[j] != '\n'; ++j)
//		aux = (aux * 10) + (buf[j] - '0');
		fscanf(fin, " %d", &aux);
		if ((t = H[aux]) == 0)
			H[aux] = t = ++W;
		A[i] = t;
	}

	fclose(fin);
}

long long least(int k) {
	int i, Q;
	int nr = 0;
	long long rez = 0;

	Q = 0;

	for (i = 0; i < N; ++i) {
		++V[A[i]];
		if (V[A[i]] == 1) ++nr;

		while (1) {
			if (nr > k || (nr == k && V[A[Q]] > 1)) {
				--V[A[Q]];
				if (V[A[Q]] == 0) --nr;
				++Q;
			} else break;
		}
		
		if (nr == k) rez += Q + 1;
	}

	for (; Q < N; ++Q) V[A[Q]] = 0;

	return rez;
}

void write() {
	FILE *fout = fopen("secv5.out", "wt");

	fprintf(fout, "%lld\n", least(L) - least(U + 1));

	fclose(fout);
}

int main() {
	
	read();

	write();

	return 0;
}