Cod sursa(job #3363577)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 19 august 2026 14:51:06
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>
#include <vector>

using namespace std;

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

struct HashTable
{
    static const int MOD = 666013;

    struct Node
    {
        int key;
        bool val;
    };
    vector<Node> H[MOD];

    inline int h(int key)
    {
        return key % MOD;
    }

    vector<Node>::iterator Find(int key)
    {
        int idx = h(key);
        vector<Node>::iterator it;
        for(it = H[idx].begin(); it != H[idx].end(); it++)
            if(it->key == key)
                return it;
        return H[idx].end();
    }

    bool FindKey(int key)
    {
        int idx = h(key);
        return Find(key) != H[idx].end();
    }

    void InsertKey(int key)
    {
        int idx = h(key);
        vector<Node>::iterator it = Find(key);
        if(it == H[idx].end())
            H[idx].push_back({.key = key, .val = true});
    }

    void DeleteKey(int key)
    {
        int idx = h(key);
        vector<Node>::iterator it = Find(key);
        if(it != H[idx].end())
            H[idx].erase(it);
    }

    bool& operator[](int key)
    {
        return Find(key)->val;
    }
};
HashTable freq;

void Solve()
{
    int q;
    fin >> q;

    while(q--)
    {
        int t, x;
        fin >> t >> x;

        switch(t)
        {
        case 1:
            freq.InsertKey(x);
            break;
        case 2:
            freq.DeleteKey(x);
            break;
        case 3:
            fout << freq.FindKey(x) << '\n';
            break;
        }
    }
}

int main()
{
    Solve();

    fin.close();
    fout.close();

    return 0;
}