Cod sursa(job #2276714)

Utilizator cezar.plescaCezar Plesca cezar.plesca Data 5 noiembrie 2018 10:56:38
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 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)
#define H_SHIFT_64 (64-H_BITS)

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

unsigned int hash64(unsigned int x) {
	long long y = (long long)x * 2654435761; 
	return y>>H_SHIFT_64;
}

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

int main()
{
	ifstream input("hashuri.in");
	ofstream output("hashuri.out");
	int i, tip, x;
 
	input >> N;
	long long y; 
	unsigned int slot;	

	for (i = 1; i <= N; i++) 
	{
		input >> tip >> x;
		//unsigned int slot = hash64(x);

		y = (long long)x * 2654435761; 
		slot=y>>H_SHIFT_64;

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