Cod sursa(job #911339)

Utilizator ELHoriaHoria Cretescu ELHoria Data 11 martie 2013 15:49:22
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <vector>

using namespace std;
 
ifstream cin("secv5.in");
ofstream cout("secv5.out");

const int mod = 667013;
vector< pair<unsigned int,int> > h[mod];
int N, L, U;
int K;
int a[1<<20], c[2][1<<20];

inline int getIndex(const unsigned int &val) {
	int hashCode = val < mod ? val : val%mod;
	for(vector < pair<unsigned int,int> >::const_iterator it = h[hashCode].begin();it != h[hashCode].end();it++) {
		if(it->first == val) {
			return it->second;
		}
	}
	h[hashCode].push_back(make_pair(val,++K));
	return K;
}

int main()
{
	cin>>N>>L>>U;
	unsigned int val;
	for(int i = 0;i < N;i++) {
		cin>>val;
		a[i] = getIndex(val);
	}
	int l, u;
	int nl, nu;
	nl = nu = 0;
	l = u = 0;
	long long ans = 0;
	for(int i = 0;i < N;i++) {
		while(l < N && nl < L) {
			nl += ((c[0][a[l++]]++) == 0);
		}
		while(u < N && nu + ( c[1][a[u]] == 0) <= U) {
			nu += ( (c[1][a[u++]]++) == 0);
		}
		if(nl >= L) {
			ans += (u - l + 1);
		}
		nl -= ((--c[0][a[i]]) == 0);
		nu -= ((--c[1][a[i]]) == 0);
	
	}
	cout<<ans;
    return 0;
}