Cod sursa(job #918293)

Utilizator Preda_IleanaPreda Ileana-Andreea Preda_Ileana Data 18 martie 2013 19:13:59
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.82 kb
//#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

using namespace std;

#define SIZE_T1    1000000
#define SIZE_T2    1000000
#define SRCH_COUNT 60
//#define PRIME      479001599
#define MIN        -2146483646


int NR;


class CuckooHash {

	public:
		CuckooHash();			
		~CuckooHash();
		void insert(int value);
		void remove(int value);
		bool containsValue(int value);


	private:
		unsigned int hash_t1(int value);
		unsigned int hash_t2(int value);
		void rehash();
		void initialize_tables();

		int *t1, *t2;
		int a,b;

};


CuckooHash::CuckooHash() {
	
	initialize_tables();
}

CuckooHash::~CuckooHash() {
	delete[] t1;
	delete[] t2;
}

void CuckooHash::initialize_tables() {

	t1 = new int[SIZE_T1];

	for(int i = 0; i < SIZE_T1; i++)
		t1[i] = MIN;

	t2 = new int[SIZE_T2];

	for(int i = 0; i < SIZE_T2; i++)
		t2[i] = MIN;
	
	srand(time(NULL));
	a = rand() % SIZE_T1;
	b = rand() % SIZE_T2;
}


void CuckooHash::rehash() {
	
	initialize_tables();
	int N, nr = 0;
	scanf("%d",&N);
	
	for(int i = 0; i < N; i++){
		int op, value;	
		scanf("%d%d",&op,&value);
		
		if(op == 1){
			if(!containsValue(value))
				insert(value);
		}
		
		if(op == 2){
			if(containsValue(value))
				remove(value);
		}
		
		if(op == 3){
			nr++;
			if(nr > NR){
				NR++;
				printf("%d\n",containsValue(value));
			}
		}
		
	}
}

void CuckooHash::insert(int value) {
	if(t1[hash_t1(value)] == MIN) {
		t1[hash_t1(value)] = value;					
		return;
	}
	if(t2[hash_t2(value)] == MIN) {
		t2[hash_t2(value)] = value;
		return;
	}

	int poz = hash_t1(value);
	
	for(int i = 0; i < SRCH_COUNT; i++) {
		if(t1[poz] == MIN) {
			t1[poz] = value;
			return;
		}

		t1[poz] ^= value;
		value ^= t1[poz];
		t1[poz] ^= value;		
		
		poz = hash_t2(value);
		if(t2[poz] == MIN) {
			t2[poz] = value;
			return;
		}

		t2[poz] ^= value;
		value ^= t2[poz];
		t2[poz] ^= value;

		poz = hash_t1(value);
	}

	rehash();
	
}
void CuckooHash::remove(int value) {
	if(t1[hash_t1(value)] == value)
		t1[hash_t1(value)] = MIN;
	if(t2[hash_t2(value)] == value)
		t2[hash_t2(value)] = MIN;
}

bool CuckooHash::containsValue(int value) {
	return t1[hash_t1(value)] == value ||
		t2[hash_t2(value)] == value;
}

unsigned int CuckooHash::hash_t1(int value) {
	
	return ( value % a);
}

unsigned int CuckooHash::hash_t2(int value) {
	return ( value % b);
}




int main() {
	

	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);

	CuckooHash x;
	int N;
	scanf("%d",&N);
	
	for(int i = 0; i < N; i++){
		int op, value;	
		scanf("%d%d",&op,&value);
		
		if(op == 1){
			if(!x.containsValue(value))
				x.insert(value);
		}
		
		if(op == 2){
			if(x.containsValue(value))
				x.remove(value);
		}
		
		if(op == 3){
			NR++;
			printf("%d\n",x.containsValue(value));
		}
		
	}
	return 0;
}