Cod sursa(job #1765580)

Utilizator demetriad-dagpagDavid Demetriad demetriad-dagpag Data 26 septembrie 2016 20:52:00
Problema Hashuri Scor 100
Compilator c Status done
Runda cerculdeinfo-lectia1-hashuri.rabinkarp Marime 1.96 kb
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1000000
#define MOD 666013
int lista[MOD],v[MAXN+1],next[MAXN+1],n;
void adauga(int x)
{
    int p;
    if(lista[x%MOD]==0)
    {
        n++;
        v[n]=x;
        next[n]=0;
        lista[x%MOD]=n;
    }
    else
    {
        p=lista[x%MOD];
        if(v[p]>=x)
        {
            if(v[p]==x);
            else
            {
                n++;
                lista[x%MOD]=n;
                next[n]=p;
                v[n]=x;
            }
        }
        else
        {
            while(next[p]!=0 && v[next[p]]<x) p=next[p];
            if(v[next[p]]==x);
            else
            {
                n++;
                next[n]=next[p];
                next[p]=n;
                v[n]=x;
            }
        }
    }
}
void sterge(int x)
{
    int p=lista[x%MOD];
    if(v[p]>=x){
        if(v[p]==x)
            lista[x%MOD]=next[p];
        else ;
    }
    else{
        while(next[p]!=0 && v[next[p]]<x) p=next[p];
        if(v[next[p]]==x)
            next[p]=next[next[p]];}
}
int cauta(int x)
{
    int p=lista[x%MOD];
    if(v[p]>=x)
        if(v[p]==x)
            return 1;
        else
            return 0;
    else{
        while(next[p]!=0 && v[next[p]]<x) p=next[p];
        if(v[next[p]]==x)
            return 1;
        else
            return 0;
    }
}
int main()
{
    int i,q,op,x,nrc=0,r;
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d",&q);
    v[0]=2000000001;
    for(i=1; i<=q; i++)
    {
        scanf("%d%d",&op,&x);
        //if(x%MOD==528981)
            //r=x%MOD;
        /*if(op==3){
            nrc++;
            if(nrc==1266)
                nrc=1266;
        }*/
        switch(op)
        {
            case 1: adauga(x); break;
            case 2: sterge(x); break;
            case 3: printf("%d\n",cauta(x)); break;
        }
    }

    return 0;
}