Cod sursa(job #3356668)

Utilizator teosimSimzianu Teodora teosim Data 3 iunie 2026 01:46:03
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;

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

    int n, l, u;
    fin >> n >> l >> u;
    vector <int> hashedElements(n);
    unordered_map <unsigned int, int> hashedElementsMap;
    hashedElementsMap.reserve(n);
    int currId = 1;

    for (int i = 0; i < n; i++)
    {
        unsigned int currElem;
        fin >> currElem;
        auto it = hashedElementsMap.find(currElem);
        if (it == hashedElementsMap.end())
        {
            hashedElementsMap[currElem] = currId;
            hashedElements[i] = currId;
            currId++;
        }
        else hashedElements[i] = it->second;
    }
    vector<int> countL(currId, 0);
    vector<int> countU(currId, 0);
    int leftU = 0, leftL = 0;
    int distinctU = 0, distinctL = 0;
    long long subsequenceCount = 0;

    for (int right = 0; right < n; right++)
    {
        int currVal = hashedElements[right];
        if (countL[currVal] == 0) distinctL++;
        countL[currVal]++;
        while (distinctL > l - 1)
        {
            int leftVal = hashedElements[leftL];
            countL[leftVal]--;
            if (countL[leftVal] == 0) distinctL--;
            leftL++;
        }

        if (countU[currVal] == 0) distinctU++;
        countU[currVal]++;
        while (distinctU > u)
        {
            int leftVal = hashedElements[leftU];
            countU[leftVal]--;
            if (countU[leftVal] == 0) distinctU--;
            leftU++;
        }
        subsequenceCount += (leftL - leftU);
    }
    fout << subsequenceCount;
    return 0;
}