Cod sursa(job #284225)

Utilizator sandyxpSanduleac Dan sandyxp Data 21 martie 2009 11:49:55
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <algorithm>
using namespace std;

#define NUME "hashuri"
ifstream fi(NUME".in");
ofstream fo(NUME".out");
#define Mod1 1000003
#define Mod2 1000033
#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 H1[Mod1], H2[Mod2];

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

void ins(hitem &hash1, hitem &hash2, int x)
{
    if (hash1.size < hash2.size)
        hash1.push(x);
    else
        hash2.push(x);
}

int main()
{
    int N, op, x;
    fi >> N;
    while (N--) {
        fi >> op >> x;
        hitem& hash1 = H1[(long long)x*P % Mod1];
        hitem& hash2 = H2[(long long)x*P % Mod2];
        switch (op) {
            case 1:
                if (!cauta(hash1, hash2, x, 0)) {
                    ins(hash1, hash2, x);
                }
                break;
            case 2:
                cauta(hash1, hash2, x, 1);
                break;
            case 3:
                fo << cauta(hash1, hash2, x) << "\n";
        }
    }
    return 0;
}