Cod sursa(job #2607189)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 29 aprilie 2020 14:43:47
Problema Secventa 5 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

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

unsigned int n,l,u,x,nrl,nru,lastl=1,lastu=1;

unsigned long long sol;

map <int, int> Ml,Mu;

queue <unsigned int> Ql,Qu;

int main()
{
    fin>>n>>l>>u;
    for(unsigned int i=1;i<=n;i++)
    {
        fin>>x;
        Ql.push(x);
        Qu.push(x);
        if(!Ml[x])
            nrl++;
        if(!Mu[x])
            nru++;
        Mu[x]++;
        Ml[x]++;
        while(!Qu.empty() && nru>u)
        {
            int elm=Qu.front();
            Qu.pop();
            Mu[elm]--;
            lastu++;
            if(!Mu[elm])
                nru--;
        }
        while(!Ql.empty())
        {
            int elm=Ql.front();
            if(nrl>l)
            {
                Ql.pop();
                Ml[elm]--;
                lastl++;
                if(!Ml[elm])
                    nrl--;
            }
            else
            {
                if(Ml[elm]>1)
                {
                    lastl++;
                    Ml[elm]--;
                    Ql.pop();
                }
                else
                    break;
            }
        }
        if(nrl>=l && nru<=u)
            sol+=lastl-lastu+1;
    }
    fout<<sol<<'\n';
}