Cod sursa(job #716126)

Utilizator BoSs_De_BosSSeFu SeFiLoR BoSs_De_BosS Data 18 martie 2012 12:43:10
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include<iostream>
#include<fstream>
#include<vector>
#define M 666013
using namespace std;

ifstream in("secv5.in");
ofstream out("secv5.out");

unsigned int n,l,u,x[1000010],st,dr;
char s[20];
vector<pair<int,int> > h[M];
vector<pair<int,int> >::iterator it;

inline void add(int el, int val) {
	
	for(it=h[el%M].begin(); it!=h[el%M].end(); ++it)
		if(it->first==el) {
			(it->second)+=val;
			return;
		}
	
	h[el%M].push_back(pair<int,int>(el,val));
}

inline int find(int el) {
	
	for(it=h[el%M].begin(); it!=h[el%M].end(); ++it)
		if(it->first==el)
			return it->second;
	
	return 0;
}

inline unsigned int sol(unsigned int dim) {
	for(int i=0;i!=M;++i)
		h[i].clear();
	if(!dim)
		return 0;
	unsigned int sol=0,nr=0;
	
	for(st=1, dr=1; dr<=n; ++dr) {
		
		if(!find(x[dr]))
			++nr;
		add(x[dr],1);
		
		while(nr>dim) {
			if(find(x[st])==1)
				--nr;
			add(x[st],-1);
			++st;
		}
		
		sol += dr - st  + 1;
	}
	return sol;
}	

int main() {
	unsigned int i,xx;
	
	in >> n >> l >> u;
	in.getline(s,30);
	
	for(i=1;i<=n;++i) {
		in.getline(s,30);
		xx = 0;
		for(int j = 0; s[j]!=0; ++j)
			xx = xx * 10 + s[j] - '0';
		x[i] = xx;
	}
	
	out << sol(u) - sol(l-1) << "\n";
	
	return 0;
}