Cod sursa(job #17654)

Utilizator VmanDuta Vlad Vman Data 16 februarie 2007 16:08:34
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <stdio.h>
#include <math.h>
#include <string.h>
#define dim_hash 100000
#define nmax 1048576
#define golden (sqrt(5)-1)/2

unsigned long n,l,u,aparitii[nmax],i,j,ind[nmax],x[nmax],key,distincte,hash[dim_hash][24],nou;
unsigned long total1,total2;
double frac;

void secvente1()
{
long j=1;
long nr=0;
for (i=1;i<=n;++i)
    {
	      if (aparitii[ind[i]]==0) ++nr;
	      ++aparitii[ind[i]];
      while (nr>l-1)
	    {
	     if (aparitii[ind[j]]==1) --nr;
	     --aparitii[ind[j]];
	     ++j;
            }
      total1 += i-j+1;
    }
}
void secvente2()
{
long j=1;
long nr=0;
for (i=1;i<=n;++i)
    {
	      if (aparitii[ind[i]]==0) ++nr;
	      ++aparitii[ind[i]];
      while (nr>u)
	    {
	     if (aparitii[ind[j]]==1) --nr;
	     --aparitii[ind[j]];
	     ++j;
            }
      total2 += i-j+1;
    }
}

int main()
{
freopen("secv5.in","r",stdin);
scanf("%lu %lu %lu\n",&n,&l,&u);
for (i=1;i<=n;++i)
    {
    scanf("%ld\n",&x[i]);
    frac=(x[i]*golden)-floor(x[i]*golden);
    key = floor( frac * dim_hash );
    nou=0;
    for (j=1;j<=hash[key][0];++j)
	if (x[i]==x[hash[key][j]]) {
				 nou=hash[key][j];
                                 break;
                                 }
    if (nou==0)
       {
       hash[key][++hash[key][0]] = i;
       ++distincte;
       ind[i] = distincte;
       }
       else ind[i]=ind[nou];
    }
fclose(stdin);
secvente1();
memset(aparitii,0,sizeof(aparitii));
secvente2();
freopen("secv5.out","w",stdout);
printf("%lu",total2-total1);
fclose(stdout);
return 0;
}