Cod sursa(job #2276695)

Utilizator cezar.plescaCezar Plesca cezar.plesca Data 5 noiembrie 2018 10:38:41
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <stdio.h>
#include <map>
#include <set>
#include <fstream>
 
using namespace std;
 
int N;

#define H_BITS 16 // Hashtable size
#define H_SHIFT (32-H_BITS)

unsigned int hash(unsigned int x) {
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = (x >> 16) ^ x;
    return x>>H_SHIFT;
}

set<int> hashtab[1<<H_BITS];  

int main()
{
	ifstream input("hashuri.in");
	ofstream output("hashuri.out");
	int i, tip, x;
 
	input >> N;
 
	for (i = 1; i <= N; i++) 
	{
		input >> tip >> x;
		unsigned int slot = hash(x);

		switch(tip){
			case 1:
				hashtab[slot].insert(x);
				break;
			case 2:
				hashtab[slot].erase(x);
				break;
			case 3:
				if(hashtab[slot].find(x) != hashtab[slot].end())
					output << "1" << endl;
				else
					output << "0" << endl;
				break;
		}
	}
 
	input.close();
	output.close();

	return 0;
}