Cod sursa(job #1243860)

Utilizator stefan.friptuPetru Stefan Friptu stefan.friptu Data 16 octombrie 2014 15:30:16
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <cstdio>
#include <algorithm>
#include <vector>

#define MOD 666013
#define NMAX (1<<20)+5

using namespace std;

unsigned long v[NMAX];
long count1;
unsigned long l,u,n;
vector <pair <unsigned long, long> > h[MOD];
vector <pair <unsigned long, long> > :: iterator it;

void insert (unsigned long key)
{
	long index=key%MOD,found=0;
	for(it=h[index].begin(); it!=h[index].end(); ++it)
	{
		if(it->first == key)
		{
			++it->second;
			if(it->second == 1)
				count1++;
			found=1;
		}
	}
	if(!found)
	{
		h[index].push_back(make_pair(key,1));
		count1++;
	}
}

void erase (unsigned long key)
{
	long index=key%MOD;
	for(it = h[index].begin(); it != h[index].end (); it ++)
	{
		if(it->first == key)
		{
			--it->second;
			if(it->second==0)
			{
				count1--;
				swap(*it,h[index].back());
				h[index].pop_back();
			}
			return;
		}
	}
}

long long nrsec (long a)
{
	long L=1,R=1,i;
	long long ans=0;
	for(i=0;i<MOD;i++)
		h[i].clear();
	count1=0;
	while(R<=n)
	{
		insert(v[R]);
		while(count1>a){
			erase(v[L]);
			L++;
		}
		R++;
		ans+=1LL*(R-L+1);
	}
	return ans;
}

int main()
{
	freopen("secv5.in","r",stdin);
	freopen("secv5.out","w",stdout);

	scanf("%ul%ul%ul",&n,&l,&u);

	for(long i=1;i<=n;i++)
		scanf("%ul",&v[i]);
	printf("%lld\n",nrsec(u)-nrsec(l-1));
    return 0;
}