Cod sursa(job #283134)

Utilizator conttPop Mircea contt Data 18 martie 2009 19:44:02
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.14 kb
#include <iostream>
#define p 1099177

using namespace std;

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

void add (int);
void remove (int);
void search (int);

NOD* list[p];
int n;

int main()
{
    int i, op, x;
 
    freopen ("hashuri.in", "r", stdin);
    freopen ("hashuri.out", "w", stdout);
    
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> op >> x;
        if (op == 1)
        {
            add (x);
        }
        else if (op == 2)
        {
            remove (x);
        }
        else
        {
            search (x);
        }
    }
    return 0;
}

void search (int x)
{
    NOD *t;
    t = list[x%p];
    while (t)
    {
        if (t->val == x)
        {
            cout << "1\n";
            return;
        }
        if (t->val<x)
        t = t->next;
        else break;
    }
    cout << "0\n";
}

void remove (int x)
{
    NOD *t, *u;
    if (!list[x%p])
    {
        return;
    }
    else
    {
        t = list[x%p];
        if (t->val == x)
        {
            list[x%p] = t->next;
            delete t;
        }
        else
        {
           
            while (1)
            {
                if (!t->next||t->val>x) return;
                u = t;
                t = t->next;
                if (t->val == x)
                { 
                    t=u;
                     u=u->next;
                     t=u->next;
                     delete u;
                    
                    return;
                }
                
            }
        }
    }
}

void add (int x)
{
    NOD *t,*q;
    if (!list[x%p])
    {
        list[x%p] = new NOD;
        t = list[x%p];
        t->val = x;
        t->next = 0;
    }
    else
    {
        t = list[x%p];
        while (1)
        {
            if (t->val == x)
                       
                return;
            
            else
            {
                if (t->next&&t->val<x)
                
                    t = t->next;
                
                else
                {   q=new NOD;
                     q->val=x;
                     q->next=t->next;
                     t->next=q;
                }
            }
        }
    }
}