Cod sursa(job #1652429)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 14 martie 2016 23:02:30
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

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

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

int n;

unsigned int v[dim], w[dim], freq[dim];

unsigned int binarySearch(unsigned int x) {

	int left = 1, right = n, ret = 0;
	while (left <= right) {

		int mid = (left + right) / 2;

		if (w[mid] < x)
			left = mid + 1;
		else {
			ret = x;
			right = mid - 1;
		}

	}

	return (unsigned int)ret;

}

long long solve(int maxCount) {

	if (maxCount == 0)
		return 0;

	long long ret = 0;
	memset(freq, 0, sizeof freq);

	for (int left = 1, right = 1, currCount = 0; right <= n; ++right) {

		++freq[v[right]];
		if (freq[v[right]] == 1)
			++currCount;

		while (currCount > maxCount && left <= right) {

			freq[v[left]]--;
			if (freq[v[left]] == 0)
				--currCount;

			++left;

		}

		ret += right - left + 1;

	}

	return ret;

}

int main() {

	int l, u;
	fin >> n >> l >> u;

	for (int i = 1; i <= n; ++i) {

		fin >> v[i];
		w[i] = v[i];

	}

	sort(w + 1, w + n + 1);

	for (int i = 1; i <= n; ++i)
		v[i] = binarySearch(v[i]);

	long long sol = solve(u) - solve(l - 1);
	
	fout << sol << '\n';

	return 0;

}

//Trust me, I'm the Doctor!