Cod sursa(job #1999534)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 11 iulie 2017 13:40:54
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.4 kb
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
const int nmax=(1<<20)+5,DIM=50000;
int poz=DIM-1;
char buff[DIM];
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;
    }
}
int v[nmax],ap[nmax];
pair<unsigned int,int>prs[nmax];
unsigned int n,l,u;
inline void citire()
{
    citeste(n),citeste(l),citeste(u);
    for(int i=1;i<=n;i++) citeste(prs[i].first),prs[i].second=i;
}
inline void norm()
{
    sort(prs+1,prs+n+1);
    int nt=0;
    for(int i=1;i<=n;i++)
    {
        if(prs[i].first!=prs[i-1].first) ++nt;
        v[prs[i].second]=nt;
    }
}
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 solve()
{
    printf("%lld\n",ans(u)-ans(l-1));
}
int main()
{
    freopen ("secv5.in","r",stdin);
    freopen ("secv5.out","w",stdout);
    citire();
    norm();
    solve();
}