Cod sursa(job #3131929)

Utilizator dra_soloSolomon Dragos dra_solo Data 21 mai 2023 21:42:07
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <vector>

#define MOD 666013

using namespace std;

vector<int> hash_table[MOD];

inline vector<int>::iterator find_hash(int x)
{
    int pos = x % MOD;
    vector<int>::iterator it;
    for (it = hash_table[pos].begin(); it != hash_table[pos].end(); it++)
    {
        if (*it == x)
            return it;
    }
    return hash_table[pos].end();
}

inline void insert_hash(int x)
{
    int pos = x % MOD;
    if (find_hash(x) == hash_table[pos].end())
        hash_table[pos].push_back(x);
}

inline void del_hash(int x)
{
    int pos = x % MOD;
    vector<int>::iterator it = find_hash(x);
    if (it != hash_table[pos].end())
        hash_table[pos].erase(it);
}

int main()
{
    ifstream input("hashuri.in");
    ofstream output("hashuri.out");
    int n, op, x;
    input >> n;
    for (int i = 1; i <= n; i++)
    {
        input >> op >> x;
        if (op == 1)
            insert_hash(x);
        else if (op == 2)
            del_hash(x);
        else if (op == 3)
        {
            if (find_hash(x) != hash_table[x % MOD].end())
                output << 1 << '\n';
            else
                output << 0 << '\n';
        }
    }
    output << '\n';
    input.close();
    output.close();
    return 0;
}