Cod sursa(job #664724)

Utilizator an_drey_curentandreycurent an_drey_curent Data 20 ianuarie 2012 18:28:30
Problema Secventa 5 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.75 kb
#include<stdio.h>
#include<vector>
#define prim 123457
using namespace std;
vector<long unsigned>hhash[123460];
vector<long unsigned>frecv[123460];
vector<long unsigned>hhash2[123460];
vector<long unsigned>frecv2[123460];
long unsigned v[1100000];
#define DIM 8192
char buff[DIM+16];
int pz;
inline void citeste(long 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(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash[cheie][i]&&frecv[cheie][i]!=0)
			return 1;
	return 0;
}
void actualizare(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash[cheie][i])
		{
			frecv[cheie][i]++;
			return;
		}
	hhash[cheie].push_back(citit);
	frecv[cheie].push_back(1);
}
void stergere(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash[cheie][i])
		{
			frecv[cheie][i]--;
			return;
		}
}
void stergere2(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash2[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash2[cheie][i])
		{
			frecv2[cheie][i]--;
			return;
		}
}
void actualizare2(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash2[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash2[cheie][i])
		{
			frecv2[cheie][i]++;
			return;
		}
	hhash2[cheie].push_back(citit);
	frecv2[cheie].push_back(1);
}
int existenta2(long unsigned citit)
{
	long unsigned i,cheie=citit%prim,lungime=hhash2[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash2[cheie][i]&&frecv2[cheie][i]!=0)
			return 1;
	return 0;
}
int main()
{
	long unsigned i,N,L,U,citit,indice_inceput=0,indice_sfarsit=0,indice_sfarsit2=0,indice_inceput2=0;
	long unsigned contor_distincte=0,contor_distincte2=0;
	long unsigned contor_U=0;
	long unsigned contor_L=0;
	freopen("secv5.in","r",stdin);
	freopen("secv5.out","w",stdout);
	scanf("%lu%lu%lu",&N,&L,&U);
	L--;
	for(i=0;i<N;i++)
	{
		citeste(v[i]);
		indice_sfarsit=i;
		indice_sfarsit2=i;
		if(contor_distincte2<=L)
			if(existenta2(v[i]))
			{
				actualizare2(v[i]);
				contor_L+=(indice_sfarsit2-indice_inceput2+1);
			}
			else
				if(!existenta2(v[i]))
				{
					actualizare2(v[i]);
					contor_distincte2++;
					if(contor_distincte2<=L)
					{
						contor_L+=(indice_sfarsit2-indice_inceput2+1);
					}
					else
						if(contor_distincte2>L)
						{
							while(contor_distincte2>L)
							{
								stergere2(v[indice_inceput2]);
								if(existenta2(v[indice_inceput2]))
									indice_inceput2++;
								else
								{
									contor_distincte2--;
									indice_inceput2++;
								}
							}
							contor_L+=(indice_sfarsit2-indice_inceput2+1);
						}
				}
		if(contor_distincte<=U)
			if(existenta(v[i]))
			{
				actualizare(v[i]);
				contor_U+=(indice_sfarsit-indice_inceput+1);
			}
			else
				if(!existenta(v[i]))
				{
					actualizare(v[i]);
					contor_distincte++;
					if(contor_distincte<=U)
					{
						contor_U+=(indice_sfarsit-indice_inceput+1);
					}
					else
						if(contor_distincte>U)
						{
							while(contor_distincte>U)
							{
								stergere(v[indice_inceput]);
								if(existenta(v[indice_inceput]))
									indice_inceput++;
								else
								{
									contor_distincte--;
									indice_inceput++;
								}
							}
							contor_U+=(indice_sfarsit-indice_inceput+1);
						}
				}
	}
	printf("%lu",contor_U-contor_L);
	return 0;
}