Cod sursa(job #496402)

Utilizator ZethpixZethpix Zethpix Data 28 octombrie 2010 21:18:07
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <stdio.h>

long i,n,a,b,x,y,m,v[1100000],A=999983,sol,nr[1100000];

struct hash
{
	long nod,aux;
	hash *link;
}*H[1000000];

void add(long x,long pos)
{
	hash *p;
	p=new hash;
	p->nod=x;
	p->aux=pos;
	p->link=H[x%A];
	H[x%A]=p;
}

long src(long x)
{
	hash *p;
	p=H[x%A];
	while(p!=NULL)
	{
		if(p->nod==x) return p->aux;
		p=p->link;
	}
	return 0;
}

long fct(long x)
{
	long pos=1,ds=0,sol=0,i;

	for(i=1;i<=m;i++) nr[i]=0;
	for(i=1;i<=n;i++)
	{
		if(nr[v[i]]==0) ds++;
		nr[v[i]]++;
		while(ds>x&&pos<=n)
		{
			if(nr[v[pos]]==1) ds--;
			nr[v[pos]]--;
			pos++;
		}
		sol+=i-pos+1;
	}
	return sol;
}

int main()
{
	freopen("secv5.in","r",stdin);
	freopen("secv5.out","w",stdout);
	scanf("%ld%ld%ld",&n,&a,&b);
	for(i=1;i<=n;i++)
	{
		scanf("%ld",&x);
		y=src(x);
		if(y==0)
		{
			add(x,++m);
			v[i]=m;
		}
		else
		v[i]=y;
	}

	sol=fct(b)-fct(a-1);
	printf("%ld\n",sol);

	return 0;
}