Cod sursa(job #1070775)

Utilizator vld7Campeanu Vlad vld7 Data 1 ianuarie 2014 23:01:39
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>
#include <map>

using namespace std;

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

const int MAX_N = (1 << 20) + 5;

int n, L, U, a[MAX_N];
int ans;
map <int, int> HashAll, Hash;

void read() {
	f >> n >> L >> U;
	for (int i = 1; i <= n; i++)
		f >> a[i];
}

void solve() {
	int p, j, done = 1;
	
	for (p = 1; p <= n && Hash.size() < L; p++)
		if (Hash.find (a[p]) == Hash.end())
			Hash.insert (make_pair (a[p], 1));
		else
			Hash[a[p]]++;
		
	for (j = 1; j <= n && done; j++)
		if (HashAll.find (a[j]) == HashAll.end() && HashAll.size() == U)
			done = 0;
		else if (HashAll.find (a[j]) == HashAll.end())
			HashAll.insert (make_pair (a[j], 1));
		else
			HashAll[a[j]]++;
		
	p--;
	if (j > n)
		j--;
	else
		j -= 2;
	
	if (Hash.size() >= L)
		ans += (j - p + 1);
	
	for (int i = 2; i <= n; i++) {
		int ok = 1;
		
		if (Hash[a[i - 1]] == 1) {
			Hash.erase (a[i - 1]);
			if (HashAll[a[i - 1]] == 1)
				HashAll.erase (a[i - 1]);
			else
				HashAll[a[i - 1]]--;
			
			
			p++;
			j++;
			
			p = max (p, i);
			j = max (j, i);
			
			while (Hash.find (a[p]) != Hash.end() && p <= n)
				Hash[a[p++]]++;
			
			while (HashAll.find (a[j]) != HashAll.end() && j <= n)
				Hash[a[j++]]++;
			
			while (a[j] == a[j + 1] && j < n)
				j++;
			
			if (p <= n)
				Hash.insert (make_pair (a[p], 1));
			else
				ok = 0;
			
			if (j <= n)
				HashAll.insert (make_pair (a[j], 1));
			else
				j = n;
		} else {
			Hash[a[i - 1]]--;
			HashAll[a[i - 1]]--;
		}
		
		if (ok && Hash.size() == L)
			ans += (j - p + 1);
	}
}

int main() {
	read();
	solve();
	
	g << ans << '\n';
	
	return 0;
}