Cod sursa(job #255569)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 9 februarie 2009 23:14:36
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
using namespace std;
#define HMAX 666013
#define amod a%HMAX

struct lista {
	lista *l; int x;
} *hash[HMAX];
int N;

void add( int a ) {
	lista *p; bool ok;
	for ( p=hash[amod], ok=false; p!=NULL && !ok; p=p->l )
		ok = p->x==a;
	if ( !ok ) {
		p = new lista;
		p -> x = a;
		p -> l = hash[ amod ];
		hash[ amod ] = p;
	}
}
void remove( int a ) {
	lista *p, *p2;
    if ( hash[amod]==NULL ) return ;
	if ( hash[ amod ] -> x == a ) {
		hash[ amod ] = hash[ amod ] -> l;
		return;
	}
	for ( p2=hash[amod], p=hash[amod]->l; p!=NULL; p2=p, p=p->l ) 
		if ( p->x == a ) {
			p2->l = p->l; 
			return;
		}
}

int query( int a ) {
	for ( lista *p=hash[amod]; p!=NULL; p=p->l )
		if ( p->x == a )
			return 1;
	return 0;
}


int main() {
	ifstream in("hashuri.in");
	ofstream out("hashuri.out");

	in >> N;
	while ( N-- ) {
		int op, x;
		in >> op >> x;
		switch ( op ) {
			case 1:
				add(x);
				break;
			case 2:
				remove(x);
                break;
			case 3:
				out << query(x) << "\n";
		}
	}
	return 0;
}