Cod sursa(job #2881437)

Utilizator ctimburCristina T ctimbur Data 30 martie 2022 15:08:40
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <cstdio>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
 
const int nmax = (1 << 20) + 5;
 
int N, L, U;
unsigned x[nmax];
vector <pair<unsigned, int>> lista;
char a[nmax];
 
long long f(int k){
	if (k == 0)
		return 0;
	int l, r, y = 0; 
    long long nr = 0;
	memset(a, 0, sizeof(a));
	for (l = r = 0; r < N; r++)
	{
		if (!a[x[r]])
			y++;
		a[x[r]]++;
		for (; y > k; l++)
		{
			a[ x[l] ]--;
			if (!a[ x[l] ])
				y--;
		}
		nr += r - l + 1;
	}
	return nr;
}
 
int main(){
	freopen("secv5.in", "rt", stdin);
	freopen("secv5.out", "wt", stdout);
	scanf("%d %d %d", &N, &L, &U);
	for (int i = 0; i < N; i++){
		scanf("%u", x + i);
		lista.push_back(make_pair(x[i], i));
	}
 
	sort(lista.begin(), lista.end());
	for (int i = 0, j = 0; i < N; i++){
		if (i > 0 && lista[i].first != lista[i - 1].first)
			j++;
		x[lista[i].second] = j;
	}
 
	printf("%lld\n", f(U) - f(L - 1));
	return 0;
}