Cod sursa(job #748855)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 14 mai 2012 23:24:44
Problema Secventa 5 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>

using namespace std;

typedef unsigned int uint;

struct str{
    unsigned int val;
    int ind;
};

char *buffer;
vector <str> h[200003];
int v[1048577],vl[1048577],vr[1048577];
char ch[16];

uint read(){
    uint x;
    while (*buffer<'0'|| *buffer>'9'){
        ++buffer;
    }

    x=0;
    while (*buffer>='0'&& *buffer<='9'){
        x=x*10+*buffer-'0';
        ++buffer;
    }

    return x;
}

int main()
{
    int fs, i,n,p,q,cnt=0,l=1,r=1,a;
    unsigned int j,x;
    long long sol=0;
    str aux;
    assert(freopen("secv5.in","r",stdin));
    fseek(stdin, 0, SEEK_END);
    fs=ftell(stdin);
    buffer=(char*)malloc(fs);
    rewind(stdin);
    assert(fread(buffer, 1, fs, stdin));
    fclose(stdin);

    assert(freopen("secv5.out","w",stdout));
    n=(int)read(); p=(int)read(); q=(int)read();
    for (i=1;i<=n;++i)
    {     
        x=read();
        a=x%200003;
        for (j=0;j<h[a].size();++j)
            if (h[a][j].val==x)
                break;
        if (j==h[a].size())
        {
            ++cnt;
            aux.ind=cnt;
            aux.val=x;
            h[a].push_back(aux);
            v[i]=cnt;
        }
        else
            v[i]=h[a][j].ind;
    }
    cnt=0;
    for (i=1;i<=n&&cnt<p;++i)
    {
        ++vl[v[i]];
        ++vr[v[i]];
        if (vl[v[i]]==1)
            ++cnt;
    }
    while (vr[v[r]]>1)
    {
        --vr[v[r]];
        ++r;
    }
    if (cnt==p)
        sol+=r;
    for (;i<=n&&cnt<q;++i)
    {
        ++vl[v[i]];
        ++vr[v[i]];
        if (vr[v[i]]==1)
        {
            if (vl[v[i]]==1)
                ++cnt;
            while (vr[v[r]]>1)
            {
                --vr[v[r]];
                ++r;
            }
            --vr[v[r]];
            ++r;
        }
        while (vr[v[r]]>1)
        {
            --vr[v[r]];
            ++r;
        }
        sol+=r;
    }
    for (;i<=n;++i)
    {
        ++vl[v[i]];
        ++vr[v[i]];
        if (vr[v[i]]==1)
        {
            while (vr[v[r]]>1)
            {
                --vr[v[r]];
                ++r;
            }
            --vr[v[r]];
            ++r;
            if (vl[v[i]]==1)
            {
                while (vl[v[l]]>1)
                {
                    --vl[v[l]];
                    ++l;
                }
                --vl[v[l]];
                ++l;
            }
        }
        while (vr[v[r]]>1)
        {
            --vr[v[r]];
            ++r;
        }
        sol+=r-l+1;
    }
    printf("%lld",sol);
    return 0;
}