Cod sursa(job #1999531)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 11 iulie 2017 13:33:59
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.17 kb
#include <cstdio>
#include <unordered_map>
#define LL long long
using namespace std;
const int nmax=(1<<20)+23,DIM=50000;
char buff[DIM];
int poz=DIM-1;
void citeste(unsigned int &nr)
{
    nr=0;
    while(!('0'<=buff[poz]&&buff[poz]<='9'))
    {
        if(++poz==DIM) fread(buff,1,DIM,stdin),poz=0;
    }
    while('0'<=buff[poz]&&buff[poz]<='9')
    {
        nr=nr*10 + buff[poz]-'0';
        if(++poz==DIM) fread(buff,1,DIM,stdin),poz=0;
    }
}
unordered_map<unsigned int,int>ap;
unsigned int v[nmax];
unsigned n,l,u;
LL ans(int X)
{
    LL st=0;
    int dif=0,j=1;
    for(int i=1;i<=n;i++)
    {
         while(j<=n)
        {
            if(ap[v[j]]==0&&dif==X) break;
            if(ap[v[j]]==0) ++dif;
            ++ap[v[j]];
            ++j;
        }
        st+=j-i;
        --ap[v[i]];
        if(ap[v[i]]==0) --dif;
        if(j<i) j=i;
    }
    return st;
}
inline void citire()
{
    citeste(n),citeste(l),citeste(u);
    for(int i=1;i<=n;i++)
    {
        citeste(v[i]);
    }
    printf("%lld\n",ans(u)-ans(l-1));
}
int main()
{
    freopen ("secv5.in","r",stdin);
    freopen ("secv5.out","w",stdout);
    citire();
}