Cod sursa(job #618440)

Utilizator rootsroots1 roots Data 15 octombrie 2011 17:19:22
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
/**********************************
*Chaining / functie hash cu random*
**********************************/

#include <fstream>
#include <cstring>
#include <ctime>
#include <cstdlib>

#define HSize 131072
#define P 15

#define MAXINT 2147483647

using namespace std;

ifstream in;
ofstream out;

struct hash
{
    int nod;
    hash *link;
}*H[HSize];

int R;

inline void createR()
{
    R=(rand()%MAXINT<<1)|1;
}

inline int convert(int val)
{
    int ret=(val*R)>>P;

    return (unsigned)ret;
}

inline int find(int val)
{
    for(hash *h=H[convert(val)];h;h=h->link)
        if(h->nod==val) return 1;
    return 0;
}

inline void ins(int val)
{
    hash *h=new hash;
    h->nod=val;
    val=convert(val);
    h->link=H[val];
    H[val]=h;
}

inline void del(int val)
{
    int pos=convert(val);

    for(hash *aux=NULL,*h=H[pos];h;aux=h,h=h->link)
        if(h->nod==val)
        {
            if(aux) aux->link=h->link;
            else H[pos]=h->link;
            delete h;
            return;
        }
}

int main()
{
    int N,x,y,ok;

    srand(time(0));
    createR();

    in.open("hashuri.in");
    out.open("hashuri.out");

    in>>N;

    for(;N--;)
    {
        in>>x>>y;

        ok=find(y);

        if(x==1&&!ok) ins(y);
        else
        if(x==2&&ok) del(y);
        else
        if(x==3) out<<ok<<'\n';
    }

    in.close();
    out.close();

    return 0;
}