Cod sursa(job #284507)

Utilizator sandyxpSanduleac Dan sandyxp Data 21 martie 2009 18:50:39
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <algorithm>
using namespace std;

#define NUME "hashuri"
ifstream fi(NUME".in");
ofstream fo(NUME".out");
#define Mod 1000003
#define P 71

struct hitem {
    int x; hitem *r;
} * H[Mod];

int cauta(hitem* &hash, int x, int del = 0)
{
    hitem *prev = 0, *p;
    for (p = hash; p && p->x != x; prev = p, p = p->r) ;
    if (p) {
        if (del) {
            if (prev)
                prev->r = p->r;
            else // e primul element...
                hash = hash->r;
            delete p;
        }
        return 1;
    }
    return 0;
}

void push(hitem* &hash, int x)
{
    hitem *elem = new hitem;
    elem->x = x; elem->r = hash;
    hash = elem;
}

//#include <iostream>
int main()
{
    int N, op, x;
    fi >> N;
    while (N--) {
        fi >> op >> x;
        //cout << op << " " << x << " -> " << (long long)x*P % Mod << "\n";
        hitem* &hash = H[(long long)x*P % Mod];
        switch (op) {
            case 1:
                if (!cauta(hash, x, 0)) {
                    push(hash, x);
                }
                break;
            case 2:
                cauta(hash, x, 1);
                break;
            case 3:
                fo << cauta(hash, x) << "\n";
        }
    }
    return 0;
}