Cod sursa(job #1090872)

Utilizator fhandreiAndrei Hareza fhandrei Data 23 ianuarie 2014 10:44:38
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;
}