Cod sursa(job #3354356)

Utilizator octavP18Podan Octvavin octavP18 Data 17 mai 2026 17:55:00
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

#define all(x) x.begin(), x.end()

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

const int nmax = (1 << 20);

int n, l, r;
int fr1[nmax + 5];
int fr2[nmax + 5];
unsigned int v[nmax + 5];

struct Element {
	unsigned int valoare;
	int pozitie_originala;

	bool operator<(const Element& other) const {
		return valoare < other.valoare;
	}
};

long long secvente(int l, int r) {
	long long secv = 0;
	int d1 = 0;
	int d2 = 0;
	for (int st = 1, dr1 = 0, dr2 = 0; st <= n; ++st) {
		while (dr1 < n && d1 < l) {
			fr1[v[++dr1]]++;
			d1 += (fr1[v[dr1]] == 1);
		}
		while (dr2 < n) {
			int urmatorul = v[dr2 + 1];
			int este_nou = (fr2[urmatorul] == 0);
			if (d2 + este_nou <= r) {
				dr2++;
				fr2[v[dr2]]++;
				d2 += este_nou;
			}
			else {
				break;
			}
		}
		if (d1 == l && d2 <= r && dr2 >= dr1) {
			secv += (long long)dr2 - dr1 + 1;
		}
		fr1[v[st]]--;
		fr2[v[st]]--;
		d1 -= (fr1[v[st]] == 0);
		d2 -= (fr2[v[st]] == 0);
		if (dr1 < st) { dr1 = st; d1 = 0; }
		if (dr2 < st) { dr2 = st; d2 = 0; }
	}
	return secv;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	if (!(fin >> n >> l >> r)) return 0;

	vector<Element> temp(n);
	for (int i = 1; i <= n; ++i) {
		fin >> v[i];
		temp[i - 1] = { v[i], i };
	}

	sort(temp.begin(), temp.end());

	int valoare_noua = 1;
	if (n > 0) {
		v[temp[0].pozitie_originala] = valoare_noua;
		for (int i = 1; i < n; ++i) {
			if (temp[i].valoare != temp[i - 1].valoare) {
				valoare_noua++;
			}
			v[temp[i].pozitie_originala] = valoare_noua;
		}
	}

	fout << secvente(l, r);
	return 0;
}