Cod sursa(job #1236560)

Utilizator andi12Draghici Andrei andi12 Data 2 octombrie 2014 09:03:28
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <cstdio>

using namespace std;
const int N=1100000;
const int M=6100007;
int v[N];
int lst[M];
int urm[N];
int val[N];
int a=0;
void ad(int x)
{
    int r=x%M;
    a++;
    urm[a]=lst[r];
    lst[r]=a;
    val[a]=x;
}
void del(int x)
{
    int r=x%M;
    int p=lst[r];
    if(x==val[p])
        lst[r]=urm[p];
    else
    {
        while(val[urm[p]]!=x)
            p=urm[p];
        urm[p]=urm[urm[p]];
    }
}
bool caut(int x)
{
    int r=x%M;
    int p=lst[r];
    while(p!=0)
    {
        if(val[p]==x)
            return true;
        p=urm[p];
    }
    return false;
}
int main()
{
    FILE *in,*out;
    in=fopen("secv5.in","r");
    out=fopen("secv5.out","w");
    int n,i,lo,hi,l,u,cnt=1,mid,f,j;
    long long ras=0;
    fscanf(in,"%d%d%d",&n,&l,&u);
    lo=1;
    hi=1;
    for(i=1;i<=n;i++)
        fscanf(in,"%d",&v[i]);
    ad(v[1]);
    mid=0;
    cnt=1;
    while(v[hi+1]!=0)
    {
        if(cnt<=u)
        {
            hi++;
            if(caut(v[hi])==false)
                cnt++;
            ad(v[hi]);
            if(cnt==l && mid==0)
                mid=hi;
        }
        if(cnt==u)
        {
            while(caut(v[hi+1])==true)
            {
                hi++;
                ad(v[hi]);
            }
            ras=ras+hi-mid+1;
            while(cnt==u)
            {
                del(v[lo]);
                if(caut(v[lo])==false)
                    cnt--;
                lo++;
                if(cnt==u)
                    ras=ras+hi-mid+1;
            }
            if(cnt<u)
            {
                f=0;
                while(f==0 && mid<=n)
                {
                    mid++;
                    for(j=mid-1;j>=lo;j--)
                        if(v[mid]!=v[j])
                            f=1;
                }
            }
        }
    }
    while(cnt>=l)
    {
        ras=ras+hi-mid+1;
        del(v[lo]);
        lo++;
        f=0;
        while(f==0 && mid<=n)
        {
            mid++;
            for(j=mid-1;j>=lo;j--)
                if(v[mid]!=v[j])
                    f=1;
        }
        if(caut(v[lo])==false)
        {
            cnt--;
        }
    }
    fprintf(out,"%lld",ras);
    return 0;
}