Cod sursa(job #3334461)

Utilizator raulthestormIlie Raul Ionut raulthestorm Data 17 ianuarie 2026 19:10:15
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <algorithm>

using namespace std;
const int NMAX = (1 << 20) + 1;

struct Element
{
	unsigned int val;
	int poz;
};

Element v[NMAX];
int N, L, U,
    w[NMAX],
    F[NMAX];

ifstream f("secv5.in");
ofstream g("secv5.out");

bool comp(const Element &x, const Element &y)
{
	return x.val < y.val;
}

void normalizare()
{
	sort(v + 1, v + N + 1, comp);
	int x = 1;
	w[v[1].poz] = x;
	for(int i = 2; i <= N; i++)
	{
		if(v[i].val != v[i - 1].val)
			x++;
		w[v[i].poz] = x;
	}
}

long long nrSecv(int K)
{
	long long NrEx = 0, sol = 0, st = 1, i;
	for(i = 1; i <= N; i++)
		F[i] = 0;
	for(i = 1; i <= N; i++)
	{
		if(++F[w[i]] == 1)
			++NrEx;
		while(NrEx > K && st <= i)
		{
			if(--F[w[st]] == 0)
				--NrEx;
			++st;
		}
		sol += i - st + 1;
	}
	return sol;
}

int main()
{
	f >> N >> L >> U;
	for(int i = 1; i <= N; i++)
	{
		f >> v[i].val;
		v[i].poz = i;
	}
	normalizare();
	g << nrSecv(U) - nrSecv(L - 1);
	f.close();
	g.close();
	return 0;
}