Cod sursa(job #1294463)

Utilizator heracleRadu Muntean heracle Data 17 decembrie 2014 16:56:32
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>

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

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

int h[Q];

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;
}