Cod sursa(job #3363601)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 19 august 2026 17:00:23
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.52 kb
#include <fstream>

using namespace std;

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

template<typename T>
struct Node
{
    T info;
    Node *prev;
    Node *next;
};

template<typename T>
struct List
{
    Node<T> *head, *tail;

    List(): head(NULL), tail(NULL) {}

    void PushBack(T info)
    {
        Node<T> *curr = new Node<T>;
        curr->info = info;
        curr->prev = tail;
        curr->next = NULL;
        if(head == NULL && tail == NULL)
        {
            head = tail = curr;
            return;
        }
        tail->next = curr;
        tail = curr;
    }

    void Erase(Node<T> *node)
    {
        if(head == tail)
            head = tail = NULL;
        else
        if(node == head)
        {
            head = head->next;
            head->prev = NULL;
        }
        else
        if(node == tail)
        {
            tail = tail->prev;
            tail->next = NULL;
        }
        else
        {
            node->prev->next = node->next;
            node->next->prev = node->prev;
        }
        delete node;
    }
};

struct HashTable
{
    static const int MOD = 666013;

    struct Entry
    {
        int key;
        bool val;
    };
    List<Entry> H[MOD];

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

    Node<Entry>* FindKey(int key)
    {
        int idx = h(key);
        Node<Entry> *it;
        for(it = H[idx].tail; it != NULL; it = it->next)
            if(it->info.key == key)
                return it;
        return NULL;
    }

    void InsertKey(int key)
    {
        int idx = h(key);
        Node<Entry> *it = FindKey(key);
        if(it == NULL)
            H[idx].PushBack({.key = key, .val = true});
    }

    void EraseKey(int key)
    {
        int idx = h(key);
        Node<Entry> *it = FindKey(key);
        if(it != NULL)
            H[idx].Erase(it);
    }

    bool& operator[](int key)
    {
        return FindKey(key)->info.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.EraseKey(x);
            break;
        case 3:
            fout << (freq.FindKey(x) != NULL) << '\n';
            break;
        }
    }
}

int main()
{
    Solve();

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

    return 0;
}