Cod sursa(job #664579)

Utilizator an_drey_curentandreycurent an_drey_curent Data 20 ianuarie 2012 13:52:37
Problema Secventa 5 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.86 kb
#include<stdio.h>
#include<vector>
#define prim  666013
using namespace std;
vector<long unsigned>hhash[666023];
vector<long unsigned>frecv[666023];
vector<long unsigned>v;
int existenta(long unsigned citit)
{
	long unsigned i,cheie=citit%prim;
	for(i=0;i<hhash[cheie].size();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;
	for(i=0;i<hhash[cheie].size();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;
		}
}
int main()
{
	long unsigned i,N,L,U,citit,indice_inceput=0,indice_sfarsit=0;
	long unsigned contor_distincte=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);
	for(i=0;i<N;i++)
	{
		scanf("%lu",&citit);
		v.push_back(citit);
		indice_sfarsit=i;
		if(contor_distincte<=U)
			if(existenta(citit))
			{
				actualizare(citit);
				contor_U+=(indice_sfarsit-indice_inceput+1);
			}
			else
				if(!existenta(citit))
				{
					actualizare(citit);
					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);
						}
				}
	}
	for(i=0;i<666018;i++)
	{
		for(long unsigned j=0;j<hhash[i].size();j++)
		{
			hhash[i][j]=0;
			frecv[i][j]=0;
		}
	}
	indice_inceput=0;
	contor_distincte=0;
	L--;
	for(i=0;i<N;i++)
	{
		indice_sfarsit=i;
		if(contor_distincte<=L)
			if(existenta(v[i]))
			{
				actualizare(v[i]);
				contor_L+=(indice_sfarsit-indice_inceput+1);
			}
			else
				if(!existenta(v[i]))
				{
					actualizare(v[i]);
					contor_distincte++;
					if(contor_distincte<=L)
					{
						contor_L+=(indice_sfarsit-indice_inceput+1);
					}
					else
						if(contor_distincte>L)
						{
							while(contor_distincte>L)
							{
								stergere(v[indice_inceput]);
								if(existenta(v[indice_inceput]))
									indice_inceput++;
								else
									if(!existenta(v[indice_inceput]))
									{
										contor_distincte--;
										indice_inceput++;
									}
							}
							contor_L+=(indice_sfarsit-indice_inceput+1);
						}
				}
	}

	printf("%lu",contor_U-contor_L);
	return 0;
}