Cod sursa(job #2141281)

Utilizator DavidDragulinDragulin David DavidDragulin Data 24 februarie 2018 11:38:38
Problema Secventa 5 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>
#include <iostream>
#include <unordered_map>
#define MAX 1<<20 + 6
using namespace std;

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

unsigned a[MAX],n,L,U;
unsigned x;

long long NrSecvente(int K)
{
    unordered_map<unsigned,int> M;
    long long cnt = 0;
    int i,j;
    i = 1;
    for(j=1; j<=n; ++j)
    {
        ++M[a[j]];
        while(M.size() > K)
        {
            x = a[i];
            ++i;
            --M[x];
            if(M[x] == 0) M.erase(x);
        }
        cnt +=j-i+1;
    }
    return cnt;
}

int main()
{
    fin>>n>>L>>U;
    for(int i=1; i<=n; ++i)
        fin>>a[i];

    fout<<NrSecvente(U) - NrSecvente(L-1);

    return 0;
}