Pagini recente » Cod sursa (job #3161006) | Cod sursa (job #3244038) | Cod sursa (job #2448063) | Cod sursa (job #848475) | Cod sursa (job #906907)
Cod sursa(job #906907)
#include <cstdio>
#include <time.h>
#include <stdlib.h>
#define nc 57
#define a1 240000
#define a2 210000
#define b1 40000
#define b2 70000
#define size 290000
using namespace std;
int const1,const2;
int f[size],val[size],i=0;
int poz1(int x)
{
return x%const1;
}
int poz2(int x)
{
return x%const2;
}
int add(int x,int *h1,int *h2)
{
int i,aux;
for(i=1;i<=nc;i++)
{
if(h1[poz1(x)]==0)
{
h1[poz1(x)]=x;
return 1;
}
else
if(h2[poz2(x)]==0)
{
h2[poz2(x)]=x;
return 1;
}
else
{
aux=h2[poz2(x)];
h2[poz2(x)]=x;
x=aux;
}
}
return 0;
}
int erase(int x,int *h1,int *h2)
{
if(h1[poz1(x)]==x)
{
h1[poz1(x)]=0;
return 1;
}
if(h2[poz2(x)]==x)
{
h2[poz2(x)]=0;
return 1;
}
return 0;
}
int find(int x,int *h1,int *h2)
{
if(h1[poz1(x)]==x || h2[poz2(x)]==x)
return 1;
return 0;
}
void rehash(int k,int *f,int *val,int *h1,int *h2)
{
int i;
const1=a1+rand()%b1;
const2=a2+rand()%b2;
for(i=1;i<=k;i++)
{
// if(f[i])
add(val[i],h1,h2);
//else
//erase(val[i],h1,h2);
}
}
int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
int n,op,x;
scanf("%d",&n);
srand(time(NULL));
const1=a1+rand()%b1;
const2=a2+rand()%b2;
int *h1 = new int[size];
int *h2 = new int[size];
while(n!=0)
{
scanf("%d%d",&op,&x);
if(op==1)
{
f[++i]=1;
val[i]=x;
if(add(x,h1,h2)==0)
rehash(i,f,val,h1,h2);
}
if(op==2)
{
f[++i]=1;
val[i]=x;
erase(x,h1,h2);
}
if(op==3)
if(find(x,h1,h2))
printf("1\n");
else printf("0\n");
n--;
}
return 0;
}