Cod sursa(job #3333672)

Utilizator StefanRaresStefan Rares StefanRares Data 14 ianuarie 2026 20:25:20
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int NMAX = (1 << 20) + 1;

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

Element v[NMAX];
int N, L, U,
    w[NMAX], ///vectorul normarilare
    F[NMAX]; ///vectorul frecventa

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 Secvente(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 << Secvente(U) - Secvente(L - 1);
    f.close();
    g.close();
    return 0;
}