Cod sursa(job #993170)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 3 septembrie 2013 14:01:39
Problema Secventa 5 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>
#include <algorithm>
#include <cstring>

#define In "secv5.in"
#define Out "secv5.out"
#define Lim 1000000
#define Nmax 1048580
#define Next ++pos == Lim ? f.read (Buffer, Lim), pos = 0 : 0
#define PII pair< int ,int >
#define val first
#define poz second

using namespace std ;

char Buffer[Lim];
int pos, p, q, n, a[Nmax], fr[Nmax];
PII v[Nmax];
ifstream f(In);

inline void Read(int &x)
{
    for(     ;!('0' <= Buffer[pos] && Buffer[pos]<='9');Next);
    for(x = 0;('0' <= Buffer[pos] && Buffer[pos]<='9');Next)
        x = x*10+Buffer[pos]-'0';
}

inline void Read()
{
    f.read(Buffer,Lim);
    Read(n);Read(p);Read(q);
    for(int i = 1;i <= n; ++i)
    {
        Read(a[i]);
        v[i].val = a[i];
        v[i].poz = i;
    }
}

inline long long Query(const int x)
{
    int Left = 1,Right = 1,cnt = 0,ret = 0;
    memset(fr,0,sizeof(fr));
    for(; Right <= n; ++Right)
    {
        if(++fr[a[Right]]==1)
            ++cnt;
        while(cnt > x)
        {
            if(--fr[a[Left]]==0)
                --cnt;
            ++Left;
        }
        ret += Right-Left+1;
    }
    return ret;
}

inline void Normalization()
{
    sort(v+1,v+n+1);
    for(int i = 1;i <= n; ++i)
        a[v[i].poz] = a[v[i-1].poz]+!(v[i].val==v[i-1].val);
}

inline void Write()
{
    ofstream g(Out);
    g<<1LL*Query(q)-1LL*Query(p-1)<<"\n";
    g.close();
}

int main()
{
    Read();
    Normalization();
    Write();
    return 0;
}