Cod sursa(job #1294467)

Utilizator heracleRadu Muntean heracle Data 17 decembrie 2014 17:06:59
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <cstdio>

FILE* in=fopen("hashuri.in","r");
FILE* out=fopen("hashuri.out","w");

const int  p1=666013,p2=300007,Q=1000007;

const int N=12000000;

int h[N+14];

int n;

void add(int x)
{
    int poz=x%p1,m=x%p2;

    while(h[poz]>0)
    {
        poz+=m;
        if(poz>N)
            poz-=N;
    }
    h[poz]=x;
}

bool exista(int x)
{
    int poz=x%p1,m=x%p2;

    while(h[poz]!=0)
    {
        if(h[poz]==x)
            return 1;
        poz+=m;

        if(poz>N)
            poz-=N;
    }
    return 0;
}

void delet(int x)
{
    int poz=x%p1,m=x%p2;

    while(h[poz]!=0)
    {
        if(h[poz]==x)
        {
            h[poz]=-1;
            return;
        }
        poz+=m;

        if(poz>N)
            poz-=N;
    }

}

int main()
{
    fscanf(in,"%d",&n);

    int t,x;

    for(int i=1; i<=n; i++)
    {
        fscanf(in,"%d%d",&t,&x);

        if(t==1)
        {
            if(!exista(x))
                add(x);
        }
        if(t==2)
        {
            delet(x);
        }
        if(t==3)
            fprintf(out,"%d\n",exista(x));
    }

    return 0;
}