Cod sursa(job #2013655)

Utilizator tanasaradutanasaradu tanasaradu Data 21 august 2017 23:18:04
Problema Secventa 5 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
const int Pmax=1000003;
const int nmax=(1<<21);
vector<pair<unsigned ,int> >L[Pmax];
int n,stg,drp,nrdist,aux;
unsigned  a[nmax];
inline void Read()
{
    fin>>n>>stg>>drp;
    aux=stg;
    for(int i=1;i<=n;i++)
            fin>>a[i];

}
inline void Push(unsigned x)
{
    bool ok=true;
    int r=x%Pmax,k;
    k=L[r].size();
    for(int i=0;i<k && ok;i++)
            if(L[r][i].first==x)
            {
                L[r][i].second++;
                ok=false;
            }
    if(ok)
    {
        nrdist++;
        L[r].push_back({x,1});
    }
}
inline void POP(unsigned  x)
{
    bool ok=true;
    int r,k;
    r=x%Pmax;
    k=L[r].size();
    for( int i=0;i<k && ok;i++)
            if(L[r][i].first==x)
    {
        L[r][i].second--;
        ok=false;
        if(!L[r][i].second)
        {
            nrdist--;
            swap(L[r][i].first,L[r][k-1].first);
            swap(L[r][i].second,L[r][k-1].second);
            L[r].pop_back();
        }
    }
}
inline long long Solve(int k)
{
    long long sol=0;
    int j=1;
    nrdist=0;
    for(int i=1;i<=Pmax;i++)
        L[i].clear();
    for(int i=1;i<=n;i++)
    {
        Push(a[i]);
        while(nrdist>k)
        {
            POP(a[j]);
            j++;
        }
        sol+=(1LL*(i-j+1));
    }
    return sol;

}
int main()
{
    Read();
    int x=Solve(drp);
    int y=Solve(aux-1);
    fout<<(x-y)<<"\n";
    fin.close();
    fout.close();
    return 0;
}