Cod sursa(job #432750)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 2 aprilie 2010 18:11:06
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <vector>
using namespace std;

typedef unsigned int uint;
const uint MOD=666013;
const int NMAX=1<<20;

#define ff first
#define ss second
#define mp make_pair
#define pb push_back

vector< pair<uint,int> > H[MOD];
int N,L,U,cnt,x[NMAX],fp[NMAX],fq[NMAX];

void add(uint x)
{
	H[x%MOD].pb(mp(x,cnt++));
}

int find(uint x)
{
	int w=x%MOD;
	vector< pair<uint,int> >::iterator it;
	for (it=H[w].begin();it!=H[w].end();++it)
		if (it->ff == x) return it->ss;
	return -1;
}

int main()
{
	ifstream fin("secv5.in");
	int i,p=0,q=0,np=0,nq=0;
	fin>>N>>L>>U;
	for (i=0;i<N;++i)
	{
		uint X;
		fin>>X;
		x[i]=find(X);
		if (x[i] == -1)
		{
			add(X);
			x[i]=cnt-1;
		}
	}
		   
	
	long long sol=0;
	for (i=0;i<N;++i)
	{
		++fp[x[i]];
		if (fp[x[i]] == 1) ++np;
		++fq[x[i]];
		if (fq[x[i]] == 1) ++nq;
		if (np < L) continue;
		
		while (nq >= L)
		  if (--fq[x[q++]] == 0) --nq;
		while (np > U)
		  if (--fp[x[p++]] == 0) --np;
		  
		sol+=q-p;
	}
		
	ofstream fout("secv5.out");
	fout<<sol;
	
    return 0;
}