Cod sursa(job #822819)

Utilizator fhandreiAndrei Hareza fhandrei Data 24 noiembrie 2012 02:01:54
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
// Include
#include <fstream>
#include <vector>
using namespace std;

// Definitii
#define pb push_back

// Constante
const int mod = 666013;

// Functii
void hashInsert(int val);
void hashDelete(int val);
bool hashCheck(int val);

// Variabile
ifstream in("hashuri.in");
ofstream out("hashuri.out");

int num;
int type, value;
vector<int> hash[mod];

// Main
int main()
{
	in >> num;
	while(num--)
	{
		in >> type >> value;
		if(type == 1)
			hashInsert(value);
		else if(type == 2)
			hashDelete(value);
		else
			out << (hashCheck(value)? 1 : 0) << '\n';
	}
	
	in.close();
	out.close();
	return 0;
}

void hashInsert(int val)
{
	int pos = val % mod;
	vector<int>::iterator it, end = hash[pos].end();
	for(it=hash[pos].begin() ; it!=end ; ++it)
		if(*it == val)
			return;
		
	hash[pos].pb(val);
}

void hashDelete(int val)
{
	int pos = val % mod;
	vector<int>::iterator it, end = hash[pos].end();
	for(it=hash[pos].begin() ; it!=end ; ++it)
		if(*it == val)
		{
			hash[pos].erase(it);
			return;
		}
}

bool hashCheck(int val)
{
	int pos = val % mod;
	vector<int>::iterator it, end = hash[pos].end();
	for(it=hash[pos].begin() ; it!=end ; ++it)
		if(*it == val)
			return true;
	
	return false;
}