Cod sursa(job #2013652)

Utilizator tanasaradutanasaradu tanasaradu Data 21 august 2017 23:12:11
Problema Secventa 5 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 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 ,int> >L[Pmax];
int n,stg,drp,nrdist;
unsigned int a[nmax];
inline void Read()
{
    fin>>n>>stg>>drp;
    for(int i=1;i<=n;i++)
            fin>>a[i];

}
inline void Push(unsigned int x)
{
    bool ok=true;
    int r=x%Pmax,k;
    k=L[r].size();
    for(unsigned 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 int x)
{
    bool ok=true;
    int r,k;
    r=x%Pmax;
    k=L[r].size();
    for(unsigned 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 void EMPTY()
{
    int i,r;
    for(int i=1;i<=n;i++)
    {
        r=a[i]%Pmax;
        while(L[r].size())
            L[r].pop_back();
    }
}
inline long long Solve(int k)
{
    long long sol=0;
    int j=1;
    nrdist=0;
    for(int i=1;i<=n;i++)
    {
        Push(a[i]);
        while(nrdist>k)
        {
            POP(a[j]);
            j++;
        }
        sol+=(i-j+1);
    }
    return sol;

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