Pagini recente » Cod sursa (job #2539980) | Cod sursa (job #1711745) | Cod sursa (job #1160969) | Cod sursa (job #1125468) | Cod sursa (job #618440)
Cod sursa(job #618440)
/**********************************
*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;
}