Cod sursa(job #2760132)

Utilizator ioana2008vIoana Velniceru ioana2008v Data 23 iunie 2021 11:21:29
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <iostream>
#include <fstream>

using namespace std;

const int P = 666019;
const int N_MAX = 1000001;
int nr_op, valori[N_MAX], liste[P + 1], urmator[N_MAX], nr_valori;

bool exista(int el){
    int cheie_hash = el % P;
    int x = liste[cheie_hash];
    while (x != -1){
        if (valori[x] == el){
            return true;
        }
        x = urmator[x];
    }
    return false;
}

void adaugare(int el){
    if (exista(el)){
        return;
    }
    int cheie_hash = el % P;
    valori[nr_valori] = el;
    urmator[nr_valori] = liste[cheie_hash];
    liste[cheie_hash] = nr_valori;
    nr_valori++;
}

void stergere(int el){
    int cheie_hash = el % P;
    int x = liste[cheie_hash];
    while (x != -1 && valori[x] != el){
        x = urmator[x];
    }
    if (x != -1){
        valori[x] = valori[liste[cheie_hash]];
        liste[cheie_hash] = urmator[liste[cheie_hash]];
    }
}

int main()
{
    ifstream fi ("hashuri.in");
    ofstream fo ("hashuri.out");
    for (int i = 0; i < P; i++)
    {
        liste[i] = -1;
    }
    fi >> nr_op;
    for (int i = 0; i < nr_op; i++){
        int tip;
        int x;
        fi >> tip;
        if (tip == 1){
            fi >> x;
            adaugare(x);
        } else if (tip == 2){
            fi >> x;
            stergere(x);
        } else {
            fi >> x;
            if (exista(x)){
                fo << "1\n";
            } else {
                fo << "0\n";
            }
        }
    }
    fi.close();
    fo.close();
    return 0;
}