Cod sursa(job #1922005)

Utilizator stelian2000Stelian Chichirim stelian2000 Data 10 martie 2017 15:38:17
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <cstdio>

using namespace std;

const int prim=103,mod=2000000;

int hashh[mod+2];

void add_hash(int x)
{
    int a=(1LL*x*prim)%mod;
    while(hashh[a]!=a && a!=-hashh[a] && hashh[a]!=0)
    {
        a++;
        if(a==mod) a=0;
    }
    hashh[a]=a;
}

void delete_hash(int x)
{
    int a=(1LL*x*prim)%mod;
    while(hashh[a]!=a && hashh[a]!=0)
    {
        a++;
        if(a==mod) a=0;
    }
    if(hashh[a]==a) hashh[a]=-a;
}

int query(int x)
{
    int a=(1LL*x*prim)%mod;
    while(hashh[a]!=a && hashh[a]!=0)
    {
        a++;
        if(a==mod) a=0;
    }
    if(hashh[a]==a) return 1;
    else return 0;
}

int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    int n,x,tip;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&tip,&x);
        if(tip==1) add_hash(x);
        else if(tip==2) delete_hash(x);
        else printf("%d\n",query(x));
    }
    return 0;
}