Pagini recente » Cod sursa (job #1364567) | Cod sursa (job #1682734) | Cod sursa (job #319635) | Cod sursa (job #1544557) | Cod sursa (job #13518)
Cod sursa(job #13518)
#include <stdio.h>
#include <string.h>
#define infile "secv5.in"
#define outfile "secv5.out"
#define NMAX 1050000
#define MOD 666013
struct NOD{unsigned int val,count; NOD *adr;};
FILE *fin,*fout;
int n,L,U;
unsigned int x[NMAX];
NOD *hash1[MOD+2],*hash2[MOD+2];
long long COUNT;
void citire()
{
int i;
fin=fopen(infile,"r");
fscanf(fin,"%d %d %d\n",&n,&L,&U);
for(i=0;i<n;i++)
fscanf(fin,"%u",&x[i]);
fclose(fin);
}
inline void adauga(NOD *hash[MOD+2], unsigned int x)
{
int poz=x%MOD;
NOD *b=hash[poz];
while(b)
{
if(b->val==x)
{
b->count++;
return;
}
b=b->adr;
}
b=new NOD;
b->val=x;
b->count=1;
b->adr=hash[poz];
hash[poz]=b;
}
inline void sterge(NOD *hash[MOD+2], unsigned int x)
{
int poz=x%MOD;
NOD *b=NULL,*c=hash[poz];
while(c)
{
if(c->val==x)
{
c->count--;
if(!c->count)
if(!b)
hash[poz]=c->adr;
else
{
b->adr=c->adr;
delete c;
}
return;
}
b=c;
c=c->adr;
}
}
inline NOD *cauta(NOD *hash[MOD+2], unsigned int x)
{
int poz=x%MOD;
NOD *b=hash[poz];
while(b)
{
if(b->val==x)
return b;
b=b->adr;
}
return NULL;
}
void solve(int DIM1, int DIM2)
{
int i,lung1=0,lung2=0,ind1=-1,ind2=0,stop=0;
int poz1,poz2;
NOD *b;
memset(hash1,0,sizeof(hash1));
memset(hash2,0,sizeof(hash2));
for(i=0;i<n;i++)
{
// cea mai scurta secventa de L nr distincte
if(!cauta(hash1,x[i]))
lung1++;
adauga(hash1,x[i]);
if(lung1==DIM1)
{
if(ind1==-1)
ind1++;
stop=0;
do{
b=cauta(hash1,x[ind1]);
if(b->count==1)
stop=1;
else
sterge(hash1,x[ind1++]);
}while(!stop);
}
else
if(lung1>DIM1)
{
stop=0;
do{
b=cauta(hash1,x[ind1]);
if(b->count==1)
{
lung1--;
stop=1;
}
sterge(hash1,x[ind1++]);
}while(!stop);
stop=0;
do{
b=cauta(hash1,x[ind1]);
if(b->count==1)
stop=1;
else
sterge(hash1,x[ind1++]);
}while(!stop);
}
poz1=ind1;
// cea mai lunga secventa de U nr distincte
if(!cauta(hash2,x[i]))
lung2++;
adauga(hash2,x[i]);
if(lung2==DIM2)
{
if(ind2==-1)
ind2=0;
}
else
if(lung2>DIM2)
{
stop=0;
do{
b=cauta(hash2,x[ind2]);
if(b->count==1)
{
lung2--;
stop=1;
}
sterge(hash2,x[ind2++]);
}while(!stop);
}
poz2=ind2;
// se adauga nr de solutii
if(poz1!=-1)
if(poz2==-1)
COUNT+=poz1+1;
else
COUNT+=poz1-poz2+1;
}
}
int main()
{
int i;
citire();
COUNT=0;
solve(L,U);
fout=fopen(outfile,"w");
fprintf(fout,"%Ld\n",COUNT);
fclose(fout);
return 0;
}