Cod sursa(job #1761524)

Utilizator antanaAntonia Boca antana Data 22 septembrie 2016 13:59:44
Problema Secventa 5 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.56 kb
#include <cstdio>
#include <ctype.h>
#define BUF_SIZE 16384
#define MAXN 1048576
#define MOD 1048575
#define uint unsigned int

uint r, pos=BUF_SIZE, v[MAXN+1], lista[MOD+1], nxt[MAXN+1], val[MAXN+1], fr[MAXN+1], a[MAXN+1], b[MAXN+1];
char buf[BUF_SIZE];

inline char getChar(FILE *f)
{
    if(pos == BUF_SIZE) pos=0, fread(buf, 1, BUF_SIZE, f);
    return buf[pos++];
}
inline uint getInt(FILE *f)
{
    uint nr=0;
    char c;

    while(!isdigit(c)) c=getChar(f);
    while(isdigit(c)) nr = nr*10 + c - '0', c=getChar(f);

    return nr;
}

inline void adauga(uint x)
{
    int mod = (x&MOD), p;
    p=lista[mod];

    while(p)
    {
        if(val[p] == x)
        {
            fr[p]++;
            return;
        }
        p=nxt[p];
    }

    val[++r]=x;
    fr[r]=1;
    nxt[r]=lista[mod];
    lista[mod]=r;
}

inline void sterge(uint x)
{
    int mod = (x&MOD), p;
    p=lista[mod];

    while(p)
    {
        if(val[p] == x)
        {
            fr[p]--;
            return;
        }
        p=nxt[p];
    }
}

inline int check(uint x)
{
    int mod = (x&MOD), p;
    p=lista[mod];

    while(p)
    {
        if(val[p] == x)
            return fr[p];
        p=nxt[p];
    }
    return 0;
}

int main()
{
    FILE *fin, *fout;
    fin = fopen("secv5.in", "r");
    fout = fopen("secv5.out", "w");

    int i, n, l, u, dif=0, st;
    long long ans=0;

    n=getInt(fin);
    l=getInt(fin);
    u=getInt(fin);

    for(i=1;i<=n;++i)
        v[i]=getInt(fin);

    i=0;
    while(dif < l)
    {
        adauga(v[++i]);
        if(check(v[i]) == 1) dif++;
    }

    a[i]=1;
    ++i;
    st=1;
    for(;i<=n;++i)
    {
        adauga(v[i]);
        if(check(v[i]) == 1) dif++;
        while(dif >= l)
        {
            sterge(v[st]);
            if(!check(v[st])) dif--;
            st++;
        }
        st--;
        adauga(v[st]);
        a[i]=st;
        dif++;
    }

    for(i=0;i<=MOD;++i)
        lista[i]=0;
    r=0;

    i=0;
    dif=0;
    while(dif < u)
    {
        b[i]=1;
        adauga(v[++i]);
        if(check(v[i]) == 1) dif++;
    }

    b[i]=1;
    ++i;
    st=1;
    for(;i<=n;++i)
    {
        adauga(v[i]);
        if(check(v[i]) == 1) dif++;
        while(dif > u)
        {
            sterge(v[st]);
            if(!check(v[st])) dif--;
            st++;
        }
        b[i]=st;
    }

    i=1;
    while(a[i] == 0) i++;
    for(;i<=n;++i)
        ans+=a[i] - b[i] + 1;

    fprintf(fout, "%lld", ans);
    return 0;
}