Cod sursa(job #1256423)

Utilizator MarronMarron Marron Data 6 noiembrie 2014 11:28:10
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>


const int MOD = 666013;
class Hash {
private:
	std::vector<int> a[MOD];

public:
	int size;
	int different;


	Hash() {
		size = 0;
		different = 0;
	}
	void clear() {
		for (int i = 0; i < MOD; i++) {
			a[i].clear();
		}
		size = 0;
		different = 0;
	}
	bool empty() {
		return (size == 0);
	}

	void insert(int x) {
		int i = x % MOD;

		bool ok = false;
		for (size_t j = 0; j < a[i].size(); j++) {
			if (a[i][j] == x) {
				ok = true;
				break;
			}
		}

		if (ok == false) {
			different++;
		}
		size++;
		a[i].push_back(x);
	}

	void erase(int x) {
		int i = x % MOD;

		for (size_t j = 0; j < a[i].size(); j++) {
			if (a[i][j] == x) {
				a[i].erase(a[i].begin() + j);
				size--;
				break;
			}
		}

		bool ok = false;
		for (size_t j = 0; j < a[i].size(); j++) {
			if (a[i][j] == x) {
				ok = true;
				break;
			}
		}

		if (ok == false) {
			different--;
		}
	}
};


std::ifstream f("secv5.in");
std::ofstream g("secv5.out");
Hash hash;
std::queue<int> q;
int a[(1 << 21) + 2], n;


int count(int val)
{
	if (val == 0) {
		return 0;
	}

	hash.clear();
	while (!q.empty()) q.pop();

	int sol = 0;
	for (int i = 1; i <= n; i++) {
		hash.insert(a[i]);
		q.push(a[i]);

		while (hash.different > val) {
			hash.erase(q.front());
			q.pop();
		}
		sol += hash.size;
	}

	return sol;
}

int main()
{
	int mn, mx;
	f >> n >> mn >> mx;
	for (int i = 1; i <= n; i++) {
		f >> a[i];
	}

	
	g << count(mx) - count(mn - 1) << std::endl;

	f.close();
	g.close();
	return 0;
}