Cod sursa(job #2809255)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 26 noiembrie 2021 14:39:50
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <unordered_map>

using namespace std;

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

#pragma GCC optimize ("Ofast")

const int maxN = (1 << 20) + 5;
int v[maxN], ans;
int n, l, u, vf[maxN], poz;
unordered_map <int, int> Hash;

long long calc(int x)
{
    long long nr_sol = 0;
    memset(vf, 0, sizeof vf);
    int cnt = 0, j = 1;
    for(int i = 1; i <= n; i++)
    {
        if(vf[v[i]] == 0)
            cnt++;
        vf[v[i]]++;
        while(cnt > x)
        {
            vf[v[j]]--;
            if(vf[v[j]] == 0)
                cnt--;
            j++;
        }
        nr_sol += 1LL*(i - j + 1);
    }
    return nr_sol;
}

int main()
{
    fin >> n >> l >> u;
    for(int i = 1; i <= n; i++)
    {
        fin >> v[i];
        if(!Hash[v[i]])
        {
            poz++;
            Hash[v[i]] = poz;
        }
        v[i] = Hash[v[i]];
    }
    long long ans = calc(u) - calc(l - 1);
    fout << ans;
    return 0;
}