Cod sursa(job #1248120)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 24 octombrie 2014 18:23:03
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
/// Craciun Catalin
///  Hashuri
#include <iostream>
#include <fstream>
#include <vector>

#define MOD 700001

using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

class Hash{
public:
    bool elementExists(int x) {
        int line = x%MOD;
        if (iteratorFor(x) != H[line].end())
            return true;

        return false;
    }
    void addElement(int x) {
        int line = x%MOD;

        if (iteratorFor(x) == H[line].end())
            H[line].push_back(x);
    }
    void eraseElement(int x) {
        int line = x%MOD;

        vector<int>::iterator it = iteratorFor(x);
        if (it != H[line].end()) {
            H[line].erase(it);
        }
    }
private:
    vector<int> H[MOD];
    vector<int>::iterator iteratorFor(int x) {
        int line = x%MOD;

        for (vector<int>::iterator it = H[line].begin(); it != H[line].end(); ++it) {
            if (*it == x)
                return it;
        }

        return H[line].end();
    }
};

int n;
Hash *customHash;

int main() {

    customHash = new Hash();
    f>>n;

    for (int i=1;i<=n;i++) {
        int type, x;
        f>>type>>x;
        if (type == 1) {
            customHash->addElement(x);
        } else if (type == 2) {
            customHash->eraseElement(x);
        } else if (type == 3) {
            g<<customHash->elementExists(x)<<'\n';
        }
    }

    f.close(); g.close();

    return 0;
}