Cod sursa(job #2820904)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 21 decembrie 2021 20:37:39
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>
 
using namespace std;
 
inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

unordered_map <unsigned int, int> H;

unsigned int v[(1 << 20) + 1], F[(1 << 20) + 1];
unsigned int N, L, U, cnt;
 
long long ans;

long long solve(int len) {
    memset(F, 0, sizeof(F));

    long long ans = 0;
    for(int i = 1, j = 1, p = 0;i <= N;i++) {
        F[v[i]]++, p += (F[v[i]] == 1);
        while(p > len)
            p -= (--F[v[j++]] == 0);
        ans += i - j + 1;
    }

    return ans;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    Open("secv5");
 
    cin >> N >> L >> U;
    for(int i = 1;i <= N;i++) {
        cin >> v[i];

        if(!H[v[i]])
            H[v[i]] = ++cnt;

        v[i] = H[v[i]];
    }
 
    cout << solve(U) - solve(L - 1);
 
    return 0;
}