Cod sursa(job #3351000)

Utilizator eric_dragosDragos Eric eric_dragos Data 15 aprilie 2026 16:46:26
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
#define N 1048580
#define MOD 666013
ll n, l, u;
unsigned int a[N];
vector<pair<unsigned int,unsigned int>> H[MOD];
vector<pair<unsigned int,unsigned int>>::iterator find_value(unsigned int x){
    unsigned int liste = x % MOD;
    for(auto it = H[liste].begin(); it != H[liste].end(); it++){
        if(it->first == x) return it;
    }
    return H[liste].end();
}
void insert_value(unsigned int x){
    unsigned int liste = x%MOD;
    auto it = find_value(x);
    if(it != H[liste].end()){
        it->second++;
        return;
    }
    H[liste].push_back({x,1});
}
int get_freq(unsigned int x){
    unsigned int liste = x % MOD;
    for(auto &p : H[liste]){
        if(p.first == x) return p.second;
    }
    return 0;
}
void erase_value(unsigned int x){
    unsigned int liste = x%MOD;
    auto it = find_value(x);
    if(it != H[liste].end()){
        it->second--;
        if(it->second == 0)
            H[liste].erase(it);
    }
}
void clear_all(){
    for(int i = 0; i<MOD; i++){
        H[i].clear();
    }
}

inline bool find_value_bool(unsigned int x){
    unsigned int liste = x%MOD;
    if(find_value(x) != H[liste].end()) return 1;
    else return 0;
}

ll nr_subsecvente_cel_mult_x_dist(int k){
    clear_all();
    int l = 1, r = 1;
    ll ans = 0;
    while(r <= n){
        if(!find_value_bool(a[r]))k--;
        insert_value(a[r]);
        while(k < 0){
            if(get_freq(a[l]) == 1) k++;
            erase_value(a[l]);
            l++;
        }
        ans += r-l+1;
        r++;
    }
    return ans;
}

int main(){
    fin >> n >> l >> u;
    for(int i = 1; i<=n; i++){
        fin >> a[i];
    }
    fout << nr_subsecvente_cel_mult_x_dist(u) - nr_subsecvente_cel_mult_x_dist(l-1);

    return 0;
}