Cod sursa(job #432737)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 2 aprilie 2010 17:50:54
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 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> > Hp[MOD],Hq[MOD];
int N,L,U;
uint x[NMAX];

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

int erase(uint x,vector< pair<uint,int> > H[])
{
	int w=x%MOD;
	vector< pair<uint,int> >::iterator it;
	for (it=H[w].begin();it!=H[w].end();++it)
		if (it->ff == x)
		{
			--it->ss;
			if (it->ss ==0)
			{
				H[w].erase(it);
				return 1;
			}
			else return 0;
		}
	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) fin>>x[i];
	
	long long sol=0;
	for (i=0;i<N;++i)
	{
		np+=add(x[i],Hp);
		nq+=add(x[i],Hq);
		if (np < L) continue;
		
		while (nq >= L)
		  if (erase(x[q++],Hq) == 1) --nq;
		while (np > U)
		  if (erase(x[p++],Hp) == 1) --np;
		  
		sol+=q-p;
	}
		
	ofstream fout("secv5.out");
	fout<<sol;
	
    return 0;
}