Pagini recente » Cod sursa (job #1708225) | Cod sursa (job #838204) | Cod sursa (job #1954421) | Cod sursa (job #1589778) | Cod sursa (job #906956)
Cod sursa(job #906956)
#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 i;
int poz1(int x)
{
return x%const1;
}
int poz2(int x)
{
return x%const2;
}
int find(int x,int *h1,int *h2)
{
if(h1[poz1(x)]==x || h2[poz2(x)]==x)
return 1;
return 0;
}
int add(int x,int *h1,int *h2)
{
int i,aux;
int care=1;
if (find(x,h1,h2) == 1)
{
return 1;
}
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
if (care==1)
{
aux=h1[poz1(x)];
h1[poz1(x)]=x;
x=aux;
}
else
{
aux=h2[poz2(x)];
h2[poz2(x)]=x;
x=aux;
}
care=-care;
}
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;
}
void rehash()
{
const1=a1+rand()%b1;
const2=a2+rand()%b2;
}
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];
int ok = 0;
while (ok == 0)
{
ok = 1;
rehash();
while(n!=0)
{
FILE *in = fopen("hashuri.in","r");
FILE *out = fopen("hashuri.out","w");
int y;
fscanf(in,"%d",&y);
fscanf(in,"%d%d",&op,&x);
if(op==1)
{
if(add(x,h1,h2)==0)
{
ok = 0;
break;
}
}
if(op==2)
{
erase(x,h1,h2);
}
if(op==3)
if(find(x,h1,h2))
fprintf(out,"1\n");
else fprintf(out,"0\n");
n--;
}
}
return 0;
}