Cod sursa(job #1090870)

Utilizator fhandreiAndrei Hareza fhandrei Data 23 ianuarie 2014 10:43:48
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 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);
int hashQuery(int val);

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

int questions;
int type, val;

vector<int> hash[mod];

// Main
int main()
{
	in >> questions;
	while(questions--)
	{
		in >> type >> val;
		switch(type)
		{
			case 1:	{	hashInsert(val); break;			}
			case 2:	{	hashDelete(val); break;			}
			case 3:	{	out << hashQuery(val) << '\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;
		}
	}
}

int hashQuery(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 1;
	
	return 0;
}