Cod sursa(job #3282959)

Utilizator inacioataCioata Ana Irina inacioata Data 7 martie 2025 17:45:10
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
int n, L, U;
unsigned int a[(1 << 20) + 3];

///returneaza nr de secvente care au cel mult x nr distincte
long long F(int x)
{
    unordered_map<unsigned int, int>fr;
    int i, j, cnt = 0;
    long long nrsecv = 0;
    i = 1;
    for(j = 1; j <= n; j++)
    {
        fr[a[j]]++;
        if(fr[a[j]] == 1) cnt++;
        while(cnt > x)
        {
            fr[a[i]]--;
            if(fr[a[i]] == 0) cnt--;
            i++;
        }
        nrsecv += (j - i + 1);
    }
    return nrsecv;
}

int main()
{
    int i;
    fin >> n >> L >> U;
    for(i = 1; i <= n; i++)
        fin >> a[i];
    fout << F(U) - F(L - 1);
    return 0;
}