Cod sursa(job #456439)

Utilizator alexandru92alexandru alexandru92 Data 15 mai 2010 17:06:05
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 15, 2010, 2:07 PM
 */
#include <cstdlib>
#include <fstream>
#define Modulo 666013
#define oo 1<<30

/*
 * 
 */
using namespace std;
struct hash
{
    int x;
    hash* next;
    hash( void ) : x(oo), next(NULL) { }
} *H[Modulo];
typedef hash* hh;
inline void Add( hh& r, int x )
{
    if( NULL == r || x == r->x )
    {
        r=new hash;
        r->x=x;
        return;
    }
    hh p;
    for( p=r; p; p=p->next )
        if( x == p->x )
            return;
    hh pp=new hash;
    pp->x=x;
    pp->next=r;
    r=pp;
}
inline bool Search( hh r, int x )
{
    for( ; r && x != r->x; r=r->next );
    return NULL != r;
}
inline void Del( hh& r, int x )
{
    if( !r )
        return;
    hh p;
    if( x == r->x )
    {
        p=r;
        r=r->next;
        delete p;
        return;
    }
    for( p=r; p->next && x != p->next->x; p=p->next );
    if( NULL == p->next || x != p->next->x )
        return;
    hh q=p->next;
    p->next=q->next;
    delete q;
}
int main( void )
{
    int N, i, j;
    ifstream in( "hashuri.in" );
    ofstream out( "hashuri.out" );
    for( in>>N; N; --N )
    {
        in>>i>>j;
        switch(i)
        {
            case 1 : Add( H[j%Modulo], j ); break;
            case 2 : Del( H[j%Modulo], j ); break;
            case 3 : out<<Search( H[j%Modulo], j )<<'\n';
        }
    }
    return EXIT_SUCCESS;
}