Cod sursa(job #2276687)

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

#define hash32(x) ((x)*2654435761)
#define H_BITS 16 // Hashtable size
#define H_SHIFT (32-H_BITS)

set<int> hashtab[1<<H_BITS];  
 
unsigned int hash(unsigned int x) {
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = (x >> 16) ^ x;
    return x;
}

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 slot = hash32(x) >> H_SHIFT;

		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;
}