Cod sursa(job #3351819)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 21 aprilie 2026 17:11:23
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <vector>
#include <unordered_map>
#include <map>

long long Rezolva(size_t t, const std::vector<unsigned int> &a)
{
    std::unordered_map<int, int> h;
    long long sol, last;
    sol = last = 0;
    int i, n = a.size();
    for(i = 0; i < n; i++)
    {
        h[a[i]]++;
        while(h.size() > t)
        {
            h[a[last]]--;
            if(h[a[last]]==0) h.erase(a[last]);
            last++;
        }
        sol += (i - last + 1);
    }
    return sol;
}

int main()
{
    std::ifstream fin("secv5.in");
    std::ofstream fout("secv5.out");
    int i, n, L, U;
    fin >> n >> L >> U;
    std::vector<unsigned int> v(n);
    for(i=0; i<n; i++)
        fin >> v[i];
    fout << Rezolva(U, v) - Rezolva(L - 1, v) << "\n";
    fin.close();
    fout.close();
    return 0;
}