Cod sursa(job #709071)

Utilizator mr.johnFMI - Laceanu Ionut-Adrian mr.john Data 7 martie 2012 17:24:16
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <stdio.h>

using namespace std;
#define MOD 1000037
#define MOD2 1000033
int n;
int T[MOD];


/*inline list<unsigned int>::iterator gaseste(unsigned int x){
	unsigned int ind=x%MOD;
	list<unsigned int>::iterator it;
	for (it=hash[ind].begin(); it!=hash[ind].end(); it++)
		if (*it==x) return it;
	return hash[ind].end();
}

void insert(unsigned int x){
	unsigned int ind=x%MOD;
	if (gaseste(x)==hash[ind].end())
		hash[ind].push_back(x);
}

void sterge(int x){
	unsigned int ind=x%MOD;
	list<unsigned int>::iterator itt=gaseste(x);
	if (itt!=hash[ind].end())
		hash[ind].erase(itt);
}
*/



int h1(int x){
	return (x%MOD);
}

int h2(int x){
	return (1+x%MOD2);
}

int h(int x, int i){
	return ((h1(x)+i*h2(x))%MOD);
}

int insert(int x){
	int temp;
	for (int i=0; i<MOD; i++){
		temp=h(x,i);
		if (T[temp]==0){
			T[temp]=x;
			return 1;
		}			
		if (T[temp]==x) return 0;
	}
}

int query(int x){
	int temp;
	for(int i=0;i<MOD;i++){
		temp=h(x,i);
		if (T[temp]==0) return 0;
		if (T[temp]==x) return 1;
	}
}

void remove(int x){
	int temp;
	for(int i=0;i<MOD;i++){
		temp=h(x,i);
		if (T[temp]==x){
			T[temp]=-1;
			return;
		}
		if (T[temp]==0) return;
	}
}

int main(){
	unsigned int op,nr;
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d %d",&op,&nr);
		if (op==1) insert(nr);
		if (op==2) remove(nr);
		if (op==3) printf("%d\n",query(nr));
	}
	return 0;
}