Cod sursa(job #2469631)

Utilizator mirceatlxhaha haha mirceatlx Data 7 octombrie 2019 19:53:53
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <vector>
#define MOD 666013
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

class Hash{
    public:
    // data structures
    vector <int> H[MOD];
    
    // methods
    vector <int> :: iterator Find(int x);
    void Insert(int x);
    void Erase(int x);

};

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

void Hash :: Insert(int x)
{
    int key = x % MOD;
    if(Find(x) == H[key].end())
        H[key].push_back(x);
}

void Hash :: Erase(int x)
{
    int key = x % MOD;
    vector <int> :: iterator it = Find(x);
    if(it != H[key].end())
        H[key].erase(it);
}

int main()
{
    Hash hash;
    vector <int> :: iterator it;
    int type, param, queries;
    fin >> queries;
    for(int i = 0 ; i < queries; i++)
    {
        fin >> type >> param;
        if(type == 1)
            hash.Insert(param);
        if(type == 2)
            hash.Erase(param);
        if(type == 3)
        {
            it = hash.Find(param);
            if(it != hash.H[param % MOD].end())
                fout << 1 << "\n";
            else
                fout << 0 << "\n";
        }
    }
    return 0;
}