Cod sursa(job #3356634)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 2 iunie 2026 20:55:32
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <algorithm>
#include <fstream>
using namespace std;

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

const int MAXN = 1000005;
int a[MAXN], freq[MAXN], n, L, U;

struct Node {
	int val, id;
} nodes[MAXN];

long long solve(int k) {
	if (k <= 0) {
		return 0;
	}
	for (int i = 0; i < n; ++i) {
		freq[i] = 0;
	}
	long long count = 0;
	int distinct = 0, left = 0;
	for (int right = 0; right < n; ++right) {
		if (freq[a[right]]++ == 0) {
			distinct++;
		}
		while (distinct > k) {
			if (--freq[a[left++]] == 0) {
				distinct--;
			}
		}
		count += (right - left + 1);
	}
	return count;
}

int main() {
	fin >> n >> L >> U;
	for (int i = 0; i < n; ++i) {
		fin >> nodes[i].val;
		nodes[i].id = i;
	}

	sort(nodes, nodes + n, [](const Node& x, const Node& y) { return x.val < y.val; });

	int m = 0;
	a[nodes[0].id] = 0;
	for (int i = 1; i < n; ++i) {
		if (nodes[i].val != nodes[i - 1].val) {
			m++;
		}
		a[nodes[i].id] = m;
	}

	fout << solve(U) - solve(L - 1) << '\n';
}