Cod sursa(job #2276738)

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

#define H_BITS 10 // 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];  

unsigned int hashrob(unsigned int a)
{
   a = (a+0x7ed55d16) + (a<<12);
   a = (a^0xc761c23c) ^ (a>>19);
   a = (a+0x165667b1) + (a<<5);
   a = (a+0xd3a2646c) ^ (a<<9);
   a = (a+0xfd7046c5) + (a<<3);
   a = (a^0xb55a4f09) ^ (a>>16);
   return a>>H_SHIFT;
}

int main()
{
	//ifstream input("hashuri.in");
	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;

		slot = hashrob(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;
}