Cod sursa(job #2809254)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 26 noiembrie 2021 14:37:28
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 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;

int calc(int x)
{
    int 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 += 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] = poz;
        }
        else
            v[i] = Hash[v[i]];
    }
    int ans = calc(u) - calc(l - 1);
    fout << ans;
    return 0;
}