Cod sursa(job #1773003)

Utilizator NinjaCubeMihai Radovici NinjaCube Data 7 octombrie 2016 13:48:18
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <cstdio>

using namespace std;

const int prim=101,mod=1<<22;
int h[mod];

void inc(int &poz)
{
    if(++poz==mod) poz=0;
}

int hash_find(int x)
{
    int poz=(1LL*x*prim)%mod;
    for(;h[poz]!=x && h[poz]!=0;inc(poz));
    if(h[poz]==x) return poz;
    else return -1;
}

void hash_add(int x)
{
    if(hash_find(x)!=-1) return;
    int poz=(1LL*x*prim)%mod;
    for(;h[poz]!=x && h[poz]>0;inc(poz));
    h[poz]=x;
}

void hash_erase(int x)
{
    int poz=hash_find(x);
    if(poz!=-1) h[poz]=-1;
}

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