Cod sursa(job #671646)

Utilizator an_drey_curentandreycurent an_drey_curent Data 31 ianuarie 2012 18:36:02
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.42 kb
#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];
unsigned chei[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,unsigned cheie)
{
	unsigned i,lungime=hhash[cheie].size();
	for(i=0;i<lungime;i++)
	{
		if(citit==hhash[cheie][i].first)
			if(hhash[cheie][i].second!=0)
			{
				hhash[cheie][i].second++;
				return 1;
			}
			else
			{
				hhash[cheie][i].second++;
				return 0;
			}
	}
	hhash[cheie].push_back(make_pair(citit,1));
	return 0;
}
int stergere(unsigned citit,unsigned cheie)
{
	unsigned i,lungime=hhash[cheie].size();
	for(i=0;i<lungime;i++)
		if(citit==hhash[cheie][i].first)
			if(hhash[cheie][i].second!=0)
			{
				hhash[cheie][i].second--;
				if(hhash[cheie][i].second==0)
					return 0;
				else
					return 1;
			}
}
int main()
{
	unsigned i,N,L,U,citit,indice_inceput=0;
	unsigned contor_distincte=0;
	long long unsigned contor_U=0;
	long long 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]);
		chei[v[i]]=v[i]%prim;
		if(!existenta(v[i],chei[v[i]]))
		{
		    contor_distincte++;
			while(contor_distincte>U)
			{
				if(stergere(v[indice_inceput],chei[v[i]]))
					indice_inceput++;
				else
					{
						contor_distincte--;
						indice_inceput++;
					}
			}
		}
		contor_U+=(long long unsigned)(i-indice_inceput+1);
	}
	for(i=indice_inceput;i<N;i++)
		stergere(v[i],chei[v[i]]);
	indice_inceput=0;
	contor_distincte=0;
	L--;
	for(i=0;i<N;i++)
	{
		if(!existenta(v[i],chei[v[i]]))
		{
			contor_distincte++;
			while(contor_distincte>L)
			{
				if(stergere(v[indice_inceput],chei[v[i]]))
					indice_inceput++;
				else
					{
						contor_distincte--;
						indice_inceput++;
					}
			}
		}
		contor_L+=(long long unsigned)(i-indice_inceput+1);
	}
	printf("%llu",contor_U-contor_L);
	return 0;
}