Cod sursa(job #3351663)

Utilizator fanceapaMosul Tudor fanceapa Data 20 aprilie 2026 19:17:06
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#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 <int, int> hashedElementsMap;
    hashedElementsMap.reserve(N);
    int currId = 1;

    for (int i = 0; i < N; i++)
    {
        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;
}