Cod sursa(job #3350047)

Utilizator iLaurianLaurian Iacob iLaurian Data 5 aprilie 2026 00:06:12
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <algorithm>
#include <fstream>
#include <vector>

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

int n, l, u, answer;

long long countAtMostK(const int k, const std::vector<unsigned int>& v, int distinct_elements) {
    if (k <= 0) return 0;

    long long count = 0;
    int left = 0;
    int distinct = 0;
    std::vector<unsigned int> fr(distinct_elements, 0);
    int n = v.size();

    for (int right = 0; right < n; right++) {
        if (fr[v[right]] == 0) {
            distinct++;
        }
        fr[v[right]]++;

        while (distinct > k && left <= right) {
            fr[v[left]]--;
            if (fr[v[left]] == 0) {
                distinct--;
            }
            left++;
        }

        count += (right - left + 1);
    }
    return count;
}

int main() {
    f >> n >> l >> u;
    std::vector<unsigned int> v;
    for (int i = 0; i < n; i++) {
        int x; f >> x;
        v.push_back(x);
    }

    std::vector<unsigned int> aux = v;
    std::sort(aux.begin(), aux.end());
    aux.erase(std::unique(aux.begin(), aux.end()), aux.end());

    int distinct_elements = aux.size();
    for (int i = 0; i < n; i++) {
        v[i] = std::lower_bound(aux.begin(), aux.end(), v[i]) - aux.begin();
    }

    g << countAtMostK(u, v, distinct_elements) - countAtMostK(l - 1, v, distinct_elements);
    return 0;
}