Cod sursa(job #2799261)

Utilizator Madalin_IonutFocsa Ionut-Madalin Madalin_Ionut Data 12 noiembrie 2021 18:37:17
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, st, dr, a[1048580], nrdist[1048580];
unordered_map<int, int> M;

/// <summary>
///  10 3 4
///                        j
/// 6 6 6 7 9 13 13 14 15 15
///         i
/// 
/// nrdist
/// 1 2 3 4 5 6 7 8 9 10
/// 1 1 1 2 3 4 4 5 6  0
///               k 
/// nrd = 4
/// cnt = 3+4+4+2+3+3 = 19
/// (k - i)
/// </summary>
/// <returns></returns>

void Citire()
{
	int i;
	fin >> n >> st >> dr;
	for (i = 1; i <= n; i++)
		fin >> a[i];
}

void Rezolvare()
{
	int i, j, nregale = 0, cnt = 0, nrd = 0, k = 1;
	i = 1;
	for (j = 1; j <= n; j++)
	{
		M[a[j]]++;
		if (M[a[j]] == 1) nrd++;
		nrdist[j] = nrdist[j - 1] + (M[a[j]] == 1);
		while (nrd > dr)
		{
			M[a[i]]--;
			if (M[a[i]] == 0) nrd--;
			i++;
		}
		if (nrd >= st)
		{
			while (nrdist[j] - nrdist[k] + 1 >= st)
				k++;
			cnt += (k - i);
		}
		nregale++;
	}
	fout << cnt << "\n";
}

int main()
{
	Citire();
	Rezolvare();
	fout.close();
	return 0;
}