Cod sursa(job #1808155)

Utilizator rangalIstrate Sebastian rangal Data 17 noiembrie 2016 13:49:08
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <cstdio>
#include <bitset>
#define in "hashuri.in"
#define out "hashuri.out"
#define mod 1047297
using namespace std;

int n;

struct nod{
    int info;
    nod *urm;
} *h[mod];

void Add(int x)
{
    int r=x%mod;
    nod *p;
    for(p=h[r]; p!=NULL && p->info !=x; p=p->urm);
    if(p==NULL)
    {
        p=new nod;
        p->info=x;
        p->urm=h[r];
        h[r]=p;
    }
}

void Del(int x)
{
    int r=x%mod;
    nod *p;
    if(h[r]==NULL) return;
    if(h[r]->info==x) // primul element
    {
        h[r]=h[r]->urm;
        return;
    }
    for(p=h[r]; p->urm!=NULL && p->urm->info !=x; p=p->urm);
    if(p->urm!=NULL)
    {
        nod *c=p->urm;
        p->urm=c->urm;
        delete c;
    }
}

bool Srch(int x)
{
    bool gasit=0;
    int r=x%mod;
    nod *p;
    for(p=h[r]; p!=NULL && !gasit; p=p->urm)
        if(p->info ==x) gasit=1;
    return gasit;
}

int main()
{
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);

    scanf("%d",&n);
    //printf("%f",sizeof(h)*1./1024/1024); //MB

    while(n--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op==1) Add(x);
        else if(op==2) Del(x);
        else printf("%d\n",Srch(x));
    }
    fclose(stdin); fclose(stdout);
    return 0;
}