Cod sursa(job #483195)

Utilizator Addy.Adrian Draghici Addy. Data 7 septembrie 2010 11:55:24
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <cstdio>
#include <cstring>

const int NMAX = 1050000;
const int MOD = 666013;

struct nod {
	unsigned int x; int v;
	nod *next;
} *HASH[MOD];

int A[NMAX], viz[NMAX], n, vmax, L, U;
long long NU, NL;

int cauta (nod *, unsigned int);
void citire (), adauga (nod *&, unsigned int), secvente ();

int main () {
	
	citire ();
	
	secvente ();
	
	return 0;
}

void citire () {
	
	freopen ("secv5.in", "r", stdin);
	
	char buffer[64];
	int i, j, v;
	unsigned int x;
	
	scanf ("%d %d %d\n", &n, &L, &U);
	
	for (i = 1; i <= n; i++) {
		scanf ("%s", buffer);
		for (x = 0, j = 0; buffer[j] >= '0' && buffer[j] <= '9'; j++)
			x = x * 10 + (buffer[j] - '0');
		
		if (v = cauta (HASH[x % MOD], x))
			A[i] = v;
		else {
			adauga (HASH[x % MOD], x);
			A[i] = vmax;
		}
	}
}

int cauta (nod *H, unsigned int x) {
	
	nod *p;
	
	for (p = H; p != NULL; p = p -> next)
		if (p -> x == x)
			return p -> v;
	
	return 0;
}

void adauga (nod *&H, unsigned int x) {
	
	nod *p = new nod;
	p -> x = x, p -> v = (++vmax), p -> next = H;
	H = p;
}

void secvente () {
	
	int p, u, distincte = 0;
	
	p = 1, u = 0;
	while (u < n) {
		
		u++, viz[ A[u] ]++;
		if (viz[ A[u] ] == 1)
			distincte++;
		
		while (distincte > U) {
			viz[ A[p] ]--;
			if (viz[ A[p] ] == 0)
				distincte--;
			
			p++;
		}
		
		NU += u - p + 1;
	}
	
	p = 1, u = 0, distincte = 0; memset (viz, 0, sizeof (viz));
	while (u < n) {
		
		u++, viz[ A[u] ]++;
		if (viz[ A[u] ] == 1)
			distincte++;
		
		while (distincte > L - 1) {
			viz[ A[p] ]--;
			if (viz[ A[p] ] == 0)
				distincte--;
			
			p++;
		}
		
		NL += u - p + 1;
	}
	
	freopen ("secv5.out", "w", stdout);
	printf ("%lld", NU - NL);
}