Pagini recente » Cod sursa (job #1029992) | Cod sursa (job #2361317) | Cod sursa (job #2238692) | Cod sursa (job #1967030) | Cod sursa (job #255569)
Cod sursa(job #255569)
#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;
}