Cod sursa(job #730025)

Utilizator teodora.petrisorPetrisor Teodora teodora.petrisor Data 1 aprilie 2012 19:18:51
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.06 kb
//============================================================================
// Name        : HashTables.cpp
// Author      : Petrisor Teodora
// Version     :
// Copyright   : 
// Description : Hash Table problem
//============================================================================

#include <stdio.h>
#include <vector>	// include vector library

using namespace std;
#define MOD 666013;

int N;
vector<int> G[MOD];	// define a vector of vectors

inline vector<int>::iterator find_value(int x)		// declare inline function that returns an iterator
{
	int list = x % MOD;				// declare key at which we search for x
	vector<int>::iterator it;		// declare iterator

	for (it = G[list].begin(); it!= G[list].end(); ++it)	// parse through values with that key
		if(*it == x)										// if we find the value we return it
			return it;

	return G[list].end();		// if not, we return the end of the list of values with the key = list
}

inline void insert_value(int x)
{
	int list = x % MOD;			// declare the key at which we search for x
	if(find_value(x) == G[list].end())  // if x is not present in list, we add it
		G[list].push_back(x);

}

inline void erase_value(int x)
{
	int list = x % MOD;			// declare the key at which we search for x
	vector<int>::iterator it = find_value(x);		// return "position" at which x is present in list

	if(it != G[list].end())		// if x is in list
		G[list].erase(it);		// remove x from list
}


int main()
{
	int op, x;

	freopen("hashuri.in", "r", stdin);		// open file for reading
	freopen("hashuri.out", "w", stdout);	// open file for writing

	for(scanf("%d", &N), N; --N)			// we read the first number and execute the loop that many times
	{
		scanf("%d %d", &op, &x);			// we read the next pair of numbers

		if(op == 1)		// we have to insert
		{
			insert_value(x);
			continue;	// go back to top of loop
		}
		if(op == 2)
		{
			erase_value(x);
			continue;	// go back to top of loop
		}

		printf("%d\n", find_value(x) ! G[x % MOD].end());	// only get here if op == 3
	}


	return 0;
}