Cod sursa(job #948210)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 9 mai 2013 18:16:27
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include <cassert>

#include <algorithm>
#include <vector>

using namespace std;

class Hash {
  public:
    Hash() {}

    void Insert(const int value) {
        int key = GetKey(value);
        if (!Search(value))
            table[key].push_back(value);
    }

    void Erase(const int value) {
        int key = GetKey(value);
        for (vector<int>::iterator v = table[key].begin(); v != table[key].end(); ++v) {
            if (*v == value) {
                table[key].erase(v);
                return;
            }
        }
    }

    bool Search(const int value) {
        int key = GetKey(value);
        for (vector<int>::iterator v = table[key].begin(); v != table[key].end(); ++v)
            if (*v == value)
                return true;
        return false;
    }

  private:
    static const int U = 666013;

    vector<int> table[U];

    static int GetKey(const int value) {
        return value % U;
    }
};

Hash H;

int main() {
    assert(freopen("hashuri.in", "r", stdin));
    assert(freopen("hashuri.out", "w", stdout));
    int N; assert(scanf("%d", &N) == 1);
    for (; N > 0; --N) {
        int Type, X; assert(scanf("%d %d", &Type, &X) == 2);
        if (Type == 1)
            H.Insert(X);
        if (Type == 2)
            H.Erase(X);
        if (Type == 3)
            printf("%d\n", H.Search(X));
    }
    return 0;
}