Cod sursa(job #803995)

Utilizator Dragan_ValentinDragan Valentin Dragan_Valentin Data 28 octombrie 2012 17:59:00
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

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


void add_item(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++)
		if (v[x%hash_n][i]==x) return;
	v[x%hash_n].push_back(x);
}

void remove_item(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++)
		if (v[x%hash_n][i]==x) {
			v[x%hash_n].erase(v[x%hash_n].begin() + i); 
			return;
		}
}

bool find(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++)
		if (v[x%hash_n][i]==x) return true;
	return false;
}


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)==true) g<<1<<'\n';
		else g<<0<<'\n';
	}
	f.close();
	g.close();
	return 0;
}