Cod sursa(job #3309841)

Utilizator Maryy_1369Gociu Maria Anastasia Maryy_1369 Data 9 septembrie 2025 17:15:06
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

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

struct HashTable {
    static const int P = 1000007;
    vector <int> table[P]; ///table[x] sunt elementele y care au h[y] = x;

    int h(int x) {
        return x % P;
    }

    bool lookup(int x) {
        ///return find(table[h(x)].begin(), table[h(x)].end(), x) != table[h(x)].end();
        int hash_value = h(x);
        for(int i = 0; i < table[hash_value].size(); i++) {
            if(x == table[hash_value][i]) {
                return true;
            }
        }

        return false;
    }

    void insert(int x) {
        if(lookup(x)) {
            return;
        }
        table[h(x)].push_back(x);
    }

    void erase(int x) {
        if(!lookup(x)) {
            return;
        }
        table[h(x)].erase(find(table[h(x)].begin(), table[h(x)].end(), x));
    }
};

HashTable H;

// set <int> H;


int main()
{
    int N;
    fin >> N;
    while(N--) {
        int t, x;
        fin >> t >> x;
        if(t == 1) {
            H.insert(x);
        }
        if(t == 2) {
            H.erase(x);
        }
        if(t == 3) {
            fout << H.lookup(x) << "\n";
        }
    }

    return 0;
}