Cod sursa(job #3182140)

Utilizator not_anduAndu Scheusan not_andu Data 8 decembrie 2023 18:06:58
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
/**
 * Author: Andu Scheusan (not_andu)
 * Created: 08.12.2023 17:12:23
*/

#include <bits/stdc++.h>
#include <unordered_map>
#pragma GCC optimize("O3")

using namespace std;

#define INFILE "secv5.in"
#define OUTFILE "secv5.out"

typedef long long ll;

const int VMAX = 1050005;

int n, lowLimit, upLimit;
int v[VMAX];

ll getMaxDistinct(int k){

    unordered_map<int, int> fr;
    int left = 1, right = 1;
    ll aux = 0;
    ll ans = 0;

    while(right <= n){

        ++fr[v[right]];

        if(fr[v[right]] == 1){
            ++aux;
        }
        ++right;

        while(aux > k){

            --fr[v[left]];

            if(fr[v[left]] == 0){
                --aux;
            }
            ++left;

        }

        ans += (right - left);

    }

    return ans;

}

void solve(){

    cin >> n >> lowLimit >> upLimit;

    for(int i = 1; i <= n; ++i) cin >> v[i];

    cout << getMaxDistinct(upLimit) - getMaxDistinct(lowLimit - 1) << '\n';

}

int main(){
    
    ios_base::sync_with_stdio(false);

    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);

    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}