Cod sursa(job #117436)

Utilizator MariusMarius Stroe Marius Data 21 decembrie 2007 14:40:06
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <stdio.h>
#include <memory.h>

const char iname[] = "secv5.in";
const char oname[] = "secv5.out";

#define MAXN  1048576
#define HASH_SIZE  1048576

typedef long long i64;

const unsigned int hash_prime = 666777;

struct hash_node {
	unsigned int key;
	int new_key;
	hash_node *next;
} *hash_map[HASH_SIZE];

inline unsigned int h(const unsigned int key) {
	return (key * hash_prime) >> 12;
}

void hash_in(unsigned int key, int new_key)
{
	hash_node *p = new hash_node;
	unsigned int n = h(key);
	p -> key = key, p -> new_key = new_key, p -> next = hash_map[n], hash_map[n] = p;
}

int hash_out(unsigned int key) 
{
	for (hash_node *p = hash_map[h(key)]; p; p = p -> next)
		if (p -> key == key)	return p -> new_key;
	return 0;
}

int N, L, U;
unsigned int A[MAXN];

int B[MAXN], Cnt[MAXN];


i64 f(const int X)
{
	if (X == 0)		
		return 0;
	memset(Cnt, 0, sizeof(Cnt));
	
	i64 ans = 0;

	for (int i = 0, head = 0, cnt = 0; i < N; ++ i) {
		cnt += ((++ Cnt[B[i]]) == 1);
		while (cnt > X) {
			cnt -= ((-- Cnt[B[head]]) == 0);
			head ++;
		}
		ans += (i64)(i-head+1);
	}
	return ans;
}        

int main(void)
{
	freopen(iname, "r", stdin);
	freopen(oname, "w", stdout);

	int key = 0, hashout;

	scanf("%d %d %d", &N, &L, &U);
	for (int i = 0; i < N; ++ i)
	{
		scanf("%u", &A[i]);
		hashout = hash_out(A[i]);
		if (!hashout)
			hash_in(A[i], (++ key)), B[i] = key;
		else
			B[i] = hashout;
	}
	printf("%lld\n", f(U) - f(L - 1));
		
	return 0;
}