Cod sursa(job #1279606)

Utilizator nytr0gennytr0gen nytr0gen Data 30 noiembrie 2014 17:11:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <cstdio>
#include <vector>
using namespace std;

const char IN_FILE[]  = "hashuri.in";
const char OUT_FILE[] = "hashuri.out";

struct Hash {
    private:
        const int MOD = 69001;
        vector<int> v[69001];

    public:
        bool insert(const int key) {
            int hash_key = key % this->MOD;

            if (!this->search(key)) {
                for (auto &it: this->v[hash_key]) {
                    if (it == -1) {
                        it = key;

                        return true;
                    }
                }

                this->v[hash_key].push_back(key);

                return true;
            }

            return false;
        }

        bool erase(const int key) {
            int hash_key = key % this->MOD;

            for (auto &it: this->v[hash_key]) {
                if (it == key) {
                    it = -1;

                    return true;
                }
            }

            return false;
        }

        bool search(const int key) {
            int hash_key = key % this->MOD;

            for (auto it: this->v[hash_key]) {
                if (it == key) {
                    return true;
                }
            }

            return false;
        }
};

int main() {
    freopen(IN_FILE, "r", stdin);
    freopen(OUT_FILE, "w", stdout);

    int N; scanf("%d", &N);

    Hash A;
    int op, key;
    while (N--) {
        scanf("%d%d", &op, &key);

        switch (op) {
            case 1:
                A.insert(key);
                break;
            case 2:
                A.erase(key);
                break;
            case 3:
                printf("%d\n", A.search(key));
                break;
        }
    }

    return 0;
}