Cod sursa(job #2766965)

Utilizator llama27Asd asd llama27 Data 4 august 2021 11:11:09
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
#include <iterator>
std::ifstream in("hashuri.in");
std::ofstream out("hashuri.out");

#define PRIME 536717

std::vector<int> table[PRIME];

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


bool exists(int x)
{
    int key = h(x);
    for(std::vector<int>::iterator it = table[key].begin();
        it!=table[key].end();
        it++)
            if(*it == x)
                return true;
    return false;
}

void insert_element(int x)
{
    int key = h(x);
    bool ok = false;
    for(std::vector<int>::iterator it = table[key].begin();
        it!=table[key].end() && !ok;
        it++)
            if(*it == x)
                ok=true;
    if(!ok)
        table[key].push_back(x);
}

void delete_element(int x)
{
    int key = h(x);
    for(std::vector<int>::iterator it = table[key].begin();
        it!=table[key].end();
        it++)
        if(*it == x){
            table[key].erase(it);
            return;
        }
}

int main()
{
    int n, t, k;
    in>>n;
    while(n--)
    {
        in>>t>>k;
        if(t==1)
            insert_element(k);
        else
            if(t==2)
               delete_element(k);
            else
                out<<exists(k)<<'\n';
    }
}