Cod sursa(job #2013647)

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

}
inline void Push(int x)
{
    bool ok=true,rest;
    int k=L[x%Pmax].size();
    for(int i=0;i<k && ok;i++)
            if(L[x%Pmax][i].first==x)
            {
                L[x%Pmax][i].second++;
                ok=false;
            }
    if(ok)
    {
        nrdist++;
        L[x%Pmax].push_back({x,1});
    }
}
inline void POP(int x)
{
    bool ok=true;
    int k=L[x%Pmax].size();
    for(int i=0;i<k && ok;i++)
            if(L[x%Pmax][i].first==x)
    {
        L[x%Pmax][i].second--;
        ok=false;
        if(!L[x%Pmax][i].second)
        {
            nrdist--;
            swap(L[x%Pmax][i].first,L[x%Pmax][k-1].first);
            swap(L[x%Pmax][x%Pmax].second,L[x%Pmax][k-1].second);
            L[x%Pmax].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;
}