Cod sursa(job #3371082)

Utilizator TimofeiFilipTimofei Filip Emanuel TimofeiFilip Data 28 octombrie 2026 15:43:22
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = (1 << 20) + 5;

unsigned int v[NMAX];
int f[NMAX];
unsigned int aux[NMAX];


void normalize(int n) {
    for (int i = 1; i <= n; i++) {
        aux[i - 1] = v[i];
    }

    sort(aux, aux + n);
    int unique_count = unique(aux, aux + n) - aux;

    for (int i = 1; i <= n; i++) {
        v[i] = lower_bound(aux, aux + unique_count, v[i]) - aux;
    }
}

unsigned long long get_prefix(int n, int max_different) {
    if (max_different < 0) return 0;
    memset(f, 0, sizeof(int) * n);

    unsigned long long result = 0;
    int current_different_numbers = 0;
    int left = 1;

    for (int right = 1; right <= n; right++) {
        unsigned int current_value = v[right];

        if (++f[current_value] == 1) {
            current_different_numbers++;
        }

        while (current_different_numbers > max_different && left <= right) {
            if (--f[v[left]] == 0) {
                current_different_numbers--;
            }
            left++;
        }

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

int main() {
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, l, r;
    if (!(cin >> n >> l >> r)) return 0;

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

    normalize(n);

    cout << get_prefix(n, r) - get_prefix(n, l - 1) << '\n';

    return 0;
}