Cod sursa(job #284430)

Utilizator sandyxpSanduleac Dan sandyxp Data 21 martie 2009 18:21:28
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 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 list[4], size;

    inline void push(int x) {
        list[size++] = x;
    }
    inline void remove(int *poz) {
        for (poz++; poz < list+size; ++poz)
            *(poz-1) = *poz;
        size --;
    }
    inline int *find(int x) {
        int *ret = std::find(list, list+size, x);
        return (ret != list+size) ? ret : 0;
    }
};
hitem H[Mod];

int cauta(hitem &hash, int x, int del = 0)
{
    if (int *i = hash.find(x)) {
        if (del) hash.remove(i);
        return 1;
    }
    return 0;
}

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