Cod sursa(job #1330816)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 30 ianuarie 2015 23:45:31
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>

using namespace std;

class Hash {

    private:
        const int size = 666013;
        vector <int> A[666013];

        int find(int value) {

            int i, normal = value % size;

            for(i = 0; i < A[normal].size(); i++)
                if(A[normal][i] == value)
                    return i;

            return -1;
        }

    public:
        Hash() {
        }

        void insert(int value) {

            int position = find(value);
            if(position == -1)
                A[value % size].push_back(value);
        }

        void remove(int value) {

            int position = find(value);
            if(position != -1)
                A[value % size].erase(A[value % size].begin() + position);

        }

        bool search(int value) {
            return (find(value) != -1);
        }
} h;

int main() {

    int type, x, queries;
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");

    in >> queries;
    while(queries--) {

        in >> type >> x;

        switch(type) {
            case 1: h.insert(x); break;
            case 2: h.remove(x); break;
            case 3: out << h.search(x) << '\n';
            }

        }

    in.close();
    out.close();

    return 0;

}