Pagini recente » Cod sursa (job #3003284) | Cod sursa (job #2631583) | Cod sursa (job #447981) | Cod sursa (job #1922447) | Cod sursa (job #915051)
Cod sursa(job #915051)
#include<fstream>
#include<time.h>
#include<iostream>
#include <stdlib.h>
#define sizemax 1000000
#define prim 23117
using namespace std;
int fact1,fact2,*hash1,*hash2;
void aloca()
{
hash1=(int*)calloc(sizemax,sizeof(int));
hash2=(int*)calloc(sizemax,sizeof(int));
}
void golire()
{
for(int i=1;i<=sizemax;i++)
{
hash1[i]=0;
hash2[i]=0;
}
}
int funct1(int x)
{
long long c;
c=(((long long) x*(long long)(fact2%10)+(long long)x%fact2)+(long long)x*5)%sizemax;
return c;
}
int funct2(int x)
{
long long c;
c=(((long long)x*((long long)(fact1%10))%prim)+(long long)x*(long long)(fact1%10))%sizemax;
return c;
}
void randomiz()
{
fact1=rand()%prim;
fact2=rand()%prim;
}
int cautare(int x)
{
if(hash1[funct1(x)]==x)return 1;
if(hash2[funct2(x)]==x)return 1;
return 0;
}
int inserare(int x)
{
if(cautare(x)==1)return 1;
int elem=0,aux;
while(elem<35)
{
if(hash1[funct1(x)]==0)
{
hash1[funct1(x)]=x;
return 1;
}
else
if(hash2[funct2(x)]==0)
{
hash2[funct2(x)]=x;
return 1;
}
else
{
elem++;
if(elem%2==1)
{
aux=hash1[funct1(x)];
hash1[funct1(x)]=x;
x=aux;
}
else
{
aux=hash2[funct2(x)];
hash2[funct2(x)]=x;
x=aux;
}
}
}
return 0;
}
void stergere(int x)
{
if(hash1[funct1(x)]==x)hash1[funct1(x)]=0;
else if(hash2[funct2(x)]==x)hash2[funct2(x)]=0;
}
void rehash(int j)
{
golire();
randomiz();
int op,x;
ifstream f("hashuri.in");
f>>op;
for(int i=1;i<=j;i++)
{
f>>op>>x;
if((op==1)&&(inserare(x)==0))rehash(j);
else if(op==2)
if(cautare(x)==1)stergere(x);
}
f.close();
}
int main()
{
srand(time(NULL));
int n,op,x;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
f>>n;
aloca();
randomiz();
for(int i=1;i<=n;i++)
{
f>>op>>x;
if((op==1)&&(inserare(x)==0))rehash(i);
if((op==2)&& (cautare(x)==1))stergere(x);
if(op==3)
{
if(cautare(x)==1)g<<"1"<<"\n";
else g<<"0"<<"\n";
}
}
return 0;
}