Cod sursa(job #346481)

Utilizator ancaaaancaaa ancaaa Data 7 septembrie 2009 23:32:13
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.29 kb
/*
	Problema Hashuri infoarena.
	http://infoarena.ro/problema/hashuri
*/

#include <iostream>

using namespace std;

#define N 1000001
#define MOD 200003
#define dim 8192

char ax[dim];
int pz;

struct nod {
	int val;
	nod *next;
};

nod *hash[MOD], *nil;
/*ifstream inFile;
ofstream outFile;*/
int n;

void init() {
	nil=new nod();
	nil->val=0;
	nil->next=0;

	for (int i=0; i<MOD; i++) {
		hash[i]=nil;
	}
}

void put_in_hash(int x, int y) {
	nod *node;
	node=new nod();
	node->val=x;
	node->next=hash[y];
	hash[y]=node;
}

bool is_in_hash(int x, int mod) {
	for (nod *i=hash[mod]; i!=nil; i=i->next) 
		if (i->val==x)
			return 1;
	return 0;
}

void del(int x, int mod) {
	if (hash[mod]->val==x) {
		nod *t=hash[mod];
		hash[mod]=hash[mod]->next;
		delete t;
	}
	for (nod *i=hash[mod]; i!=nil; i=i->next)
		if (i->next->val==x) {
			nod *t=i->next;
			i->next=t->next;
			delete t;
			return;
		}
}

/*void print_list(nod *P) {
	for (nod *i=P; i!=nil; i=i->next)
		cout<<i->val<<" ";
	cout<<endl;
}

void print_hash() {
	// tiparesc numai primele 5 intrari din hash
	cout<<"Hash-ul este: "<<endl;
	for (int i=0; i<=5; i++)
		if (hash[i]!=nil) {
			cout<<i<<": ";
			print_list(hash[i]);
		}
	cout<<"...";
}*/

inline void cit(int &x) {
       x  = 0;
       while(ax[pz] < '0' || ax[pz] > '9') 
           if(++pz == dim) fread(ax,1,dim,stdin), pz = 0;
       while(ax[pz] >= '0' && ax[pz] <= '9')
       {
              x = x*10 + ax[pz]-'0';
              if(++pz == dim) fread(ax,1,dim,stdin),pz=0;
      }
}


int main() {
	int x, y, mod;

	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	/*inFile.open("hashuri.in", ios::in);
	if (inFile.is_open()==0) exit(1);

	outFile.open("hashuri.out", ios::out);
	if (outFile.is_open()==0) exit(1);*/
	
	init();
	
	//inFile>>n;
	cit(n);
	for (int i=1; i<=n; i++) {
		/*inFile>>y;
		inFile>>x;*/
		cit(y);
		cit(x);
		mod=x%MOD;

		switch (y) {
			case 1: {
						if (is_in_hash(x, mod)==0)
							put_in_hash(x, mod);

						break;
			}

			case 2: {
						del(x, mod);
							
						break;
			}

			case 3: {
						bool b=is_in_hash(x, mod);
						cout<<b<<endl;

						break;
			}

		}
	}
	
	//print_hash();

	return 0;
}