Cod sursa(job #1764599)

Utilizator tiberiu.bucur17Tiberiu Constantin Emanoil Bucur tiberiu.bucur17 Data 25 septembrie 2016 18:06:23
Problema Secventa 5 Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <stdio.h>
#define MOD 2093570
#define MAX_N 1048576
unsigned int v[MAX_N],val[2*MAX_N+1];
int hash1[MOD],hash2[MOD],next[2*MAX_N+1],frecv[2*MAX_N+1],m;
int caut(unsigned int x,int *e)
{
    int p;
    p=*e;
    while(p && val[p]!=x)
        p=next[p];
    return p;
}
void add(unsigned int x,int *e)
{
    int p;
    p=caut(x,e);
    if(!p)
    {
        val[++m]=x;
        frecv[m]=1;
        next[m]=*e;
        *e=m;
    }
    else
        frecv[p]++;
}
void sterg(unsigned int x,int list[],int *e)
{
    int p;
    p=list[x%MOD];
    if(val[p]==x)
    {
        frecv[p]--;
        if(!frecv[p])
        {
            (*e)--;
            list[x%MOD]=next[list[x%MOD]];
        }
    }
    else
    {
        while(next[p] && val[next[p]]!=x)
            p=next[p];
        if(next[p])
        {
            frecv[next[p]]--;
            if(!frecv[next[p]])
            {
                (*e)--;
                next[p]=next[next[p]];
            }
        }
    }
}
int main()
{
    FILE *fin,*fout;
    fin=fopen("secv5.in","r");
    fout=fopen("secv5.out","w");
    int n,l,u,a,b,co,i,p,q;
    fscanf(fin,"%d%d%d",&n,&l,&u);
    for(i=0;i<n;i++)
        fscanf(fin,"%d",&v[i]);
    a=b=p=q=co=0;
    for(i=0;i<n;i++)
    {
        while(a<l && p<n)
        {
            if(!caut(v[p],&hash1[v[p]%MOD]))
                a++;
            add(v[p],&hash1[v[p]%MOD]);
            p++;
        }
        while(q<n && (b<u || (b==u && caut(v[q],&hash2[v[q]%MOD]))))
        {
            if(!caut(v[q],&hash2[v[q]%MOD]))
                b++;
            add(v[q],&hash2[v[q]%MOD]);
            q++;
        }
        if(a==l)
            co+=q-p+1;
        sterg(v[i],hash1,&a);
        sterg(v[i],hash2,&b);
    }
    fprintf(fout,"%d",co);
    fclose(fin);
    fclose(fout);
    return 0;
}