Cod sursa(job #2467064)

Utilizator Vlad.Vlad Cristian Vlad. Data 3 octombrie 2019 17:22:11
Problema Secventa 5 Scor 90
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <iostream>
#include <unordered_map>
#define MAX_N (1 << 20) + 1
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
int n, u, l;
unordered_map<unsigned int, unsigned int> mapU, mapL;
unsigned int v[MAX_N];
long long con;
void citire() {
    fin >> n >> l >> u;
    for (int i = 0; i < n; ++i) {
        fin >> v[i];
    }
}
int main()
{
    citire();
    // poz1 -> i l-1 nr distincte
    // poz2 -> i u nr distincte
    long long poz1 = 0, poz2 = 0;
    for (int i = 0; i < n; ++i) {
        mapL[v[i]]++;
        mapU[v[i]]++;
        while (mapL.size() >= l) {
            mapL[v[poz1]]--;
            if (mapL[v[poz1]] == 0) {
                mapL.erase(v[poz1]);
            }
            poz1++;
        }
        while (mapU.size() > u) {
            mapU[v[poz2]]--;
            if (mapU[v[poz2]] == 0) {
                mapU.erase(v[poz2]);
            }
            poz2++;
        }
        con += i - poz2;
        con -= i - poz1;
    }
    fout << con << '\n';
    return 0;
}