Cod sursa(job #804009)

Utilizator Dragan_ValentinDragan Valentin Dragan_Valentin Data 28 octombrie 2012 18:08:39
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

int hash_n;
vector<vector<int> > v;

inline int find(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++)
		if (v[x%hash_n][i]==x) return i;
	return -1;
}


inline void add_item(int x)
{
	if (find(x)==-1) v[x%hash_n].push_back(x);
}


inline void remove_item(int x)
{
	int aux=find(x);
	if (aux!=-1) v[x%hash_n].erase(v[x%hash_n].begin() + aux);
}




inline void create_vector(int n)
{
	hash_n=sqrt((float)n)*log2(n);
	v.resize(hash_n);
	for (int i=0; i<v.size(); i++)
		v[i]=vector<int>();
}

int main()
{
	int i,n;
	ifstream f("hashuri.in");
	ofstream g("hashuri.out");
	f>>n;
	
	create_vector(n);

	int x;
	int oper;
	for (i=0; i<n; i++) {
		f>>oper>>x;
		if (oper==1) add_item(x);
		else if (oper==2) remove_item(x); 
		else if (oper==3 && find(x)!= -1) g<<1<<'\n';
		else g<<0<<'\n';
	}
	f.close();
	g.close();
	return 0;
}