Cod sursa(job #2607210)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 29 aprilie 2020 15:17:45
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.71 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

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

const int MOD = 200117;

vector < pair<int,int> > Hashl[MOD],Hashu[MOD];

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

unsigned long long sol;

queue <unsigned int> Ql,Qu;

void FindHash(vector < pair<int,int> > Hash[], int type, int value)
{
    int l=value%MOD;
    bool ok=0;
    vector < pair<int,int> > ::iterator it;
    for(it=Hash[l].begin();it!=Hash[l].end();it++)
    {
        if((*it).first==value)
        {
            if(type==1)
            {
                ok=1;
                (*it).second++;
                return;
            }
            else if(type==2)
            {
                IsOk=1;
                return;
            }
            else if(type==3)
            {
                int ans=(*it).second;
                if(ans>1)
                    (*it).second--;
                else
                    Hash[l].erase(it);
                return;
            }
            else
            {
                qnt=(*it).second;
                return;
            }
        }
    }
    if(type==1 && !ok)
        Hash[l].push_back(make_pair(value,1));
}

int main()
{
    fin>>n>>l>>u;
    for(unsigned int i=1;i<=n;i++)
    {
        fin>>x;
        Ql.push(x);
        Qu.push(x);
        IsOk=0;
        FindHash(Hashl,2,x);
        if(!IsOk)
            nrl++;
        IsOk=0;
        FindHash(Hashu,2,x);
        if(!IsOk)
            nru++;
        FindHash(Hashu,1,x);
        FindHash(Hashl,1,x);
        while(!Qu.empty() && nru>u)
        {
            int elm=Qu.front();
            Qu.pop();
            FindHash(Hashu,3,elm);
            lastu++;
            IsOk=0;
            FindHash(Hashu,2,elm);
            if(!IsOk)
                nru--;
        }
        while(!Ql.empty())
        {
            int elm=Ql.front();
            if(nrl>l)
            {
                Ql.pop();
                IsOk=0;
                FindHash(Hashl,3,elm);
                lastl++;
                FindHash(Hashl,2,elm);
                if(!IsOk)
                    nrl--;
            }
            else
            {
                qnt=0;
                FindHash(Hashl,4,elm);
                if(qnt>1)
                {
                    lastl++;
                    FindHash(Hashl,3,elm);
                    Ql.pop();
                }
                else
                    break;
            }
        }
        if(nrl>=l && nru<=u)
            sol+=lastl-lastu+1;
    }
    fout<<sol<<'\n';
}