Pagini recente » Cod sursa (job #263652) | Cod sursa (job #388788) | Cod sursa (job #2689788) | Cod sursa (job #488912) | Cod sursa (job #671448)
Cod sursa(job #671448)
#include<stdio.h>
#include<vector>
#include<algorithm>
#define prim 123457
#define DIM 10
using namespace std;
vector<pair<unsigned,unsigned> >hhash[123460];
unsigned v[1100000];
char buff[DIM+16];
int pz;
inline void citeste(unsigned &x)
{
x=0;
while((buff[pz]<'0' || buff[pz]>'9') && (buff[pz]!='-'))
if(++pz==DIM)fread(buff, 1, DIM, stdin), pz=0;
while(buff[pz]>='0' && buff[pz]<='9')
{
x=x*10+buff[pz]-'0';
if(++pz==DIM)fread(buff,1, DIM, stdin),pz=0;
}
}
int existenta(unsigned citit)
{
vector<pair<unsigned,unsigned> >::iterator it;
unsigned i=0,cheie=citit%prim;
for(it=hhash[cheie].begin();it!=hhash[cheie].end();it++)
{
if(citit==(*it).first &&(*it).second!=0)
{
hhash[cheie][i].second++;
return 1;
}
if(citit==(*it).first&&(*it).second==0)
{
hhash[cheie][i].second++;
return 0;
}
i++;
}
pair<unsigned,unsigned>aux;
aux=make_pair(citit,1);
hhash[cheie].push_back(aux);
return 0;
}
int stergere(unsigned citit)
{
unsigned i=0,cheie=citit%prim;
vector<pair<unsigned,unsigned> >::iterator it;
for(it=hhash[cheie].begin();it!=hhash[cheie].end();it++)
{
if(citit==(*it).first &&(*it).second!=0)
{
hhash[cheie][i].second--;
if(hhash[cheie][i].second==0)
return 0;
else
return 1;
}
i++;
}
}
int main()
{
unsigned x,y,i,N,L,U,citit,indice_inceput=0,indice_sfarsit=0;
unsigned contor_distincte=0;
unsigned contor_U=0;
unsigned contor_L=0;
freopen("secv5.in","r",stdin);
freopen("secv5.out","w",stdout);
scanf("%u%u%u",&N,&L,&U);
for(i=0;i<N;i++)
{
citeste(v[i]);
y=existenta(v[i]);
if(!y)
contor_distincte++;
while(contor_distincte>U)
{
x=stergere(v[indice_inceput]);
if(x)
indice_inceput++;
else
{
contor_distincte--;
indice_inceput++;
}
}
contor_U+=(i-indice_inceput+1);
}
for(i=0;i<123459;i++)
hhash[i].clear();
indice_inceput=0;
contor_distincte=0;
L--;
for(i=0;i<N;i++)
{
y=existenta(v[i]);
if(!y)
contor_distincte++;
while(contor_distincte>L)
{
x=stergere(v[indice_inceput]);
if(x)
indice_inceput++;
else
{
contor_distincte--;
indice_inceput++;
}
}
contor_L+=(i-indice_inceput+1);
}
printf("%u",contor_U-contor_L);
return 0;
}