Cod sursa(job #3351024)

Utilizator aspaAlexandru Valentin Grigorescu aspa Data 15 aprilie 2026 18:27:20
Problema Secventa 5 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main(){

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

    unsigned int nums[1<<20];
    unsigned int N, L, U, l, r, count1 = 0, count2 = 0;
    unordered_map<unsigned int, unsigned int> map;

    fin>>N>>L>>U;
    for(unsigned int i=0; i < N; i++)
        fin>>nums[i];

    // two pointers to find all subarrays with <= L distinct elements
    l = r = 0;
    while(r < N){
        map[nums[r]]++;
        while(map.size() > L-1){
            map[nums[l]]--;
            if(map[nums[l]] == 0)
                map.erase(nums[l]);
            l++;
        }
        count1 += r - l + 1;
        r++;
    }
    
    map.clear();
    // two pointers to find all subarrays with <= U distinct elements
    l = r = 0;
    while(r < N){
        map[nums[r]]++;
        while(map.size() > U){
            map[nums[l]]--;
            if(map[nums[l]] == 0)
                map.erase(nums[l]);
            l++;
        }
        count2 += r - l + 1;
        r++;
    }

    // func(<=U) - func(<=L) = func(>=L && <=U), where "-" is set difference, with func(<=U) and func(<=L) sets.
    fout<<count2 - count1;

    return 0;
}