Cod sursa(job #3129252)

Utilizator Adela_PetrePetre Adela Adela_Petre Data 13 mai 2023 15:43:43
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");
const int valueHash = 666100;
vector<int> Hash[valueHash + 1];
int N, comanda, x;

int findInHash(int x) { //functie care determina daca valoarea x se afla in hash
    int xHash = x % valueHash;
    for (int j = 0; j < Hash[xHash].size(); j++) {
        if (Hash[xHash][j] == x)
            return j; // daca il gaseste returneaza pozitia lui
    }
    return -1;
}
int main(){
    in >> N;
    for (int i = 0; i < N; i++) {
        in >> comanda >> x;
        if (comanda == 1) { //il vom insera pe x in hash daca nu este deja stocat
            if (findInHash(x) == -1)
                Hash[x % valueHash].push_back(x);
        } else if (comanda == 2) { //stergem pe x
            int poz = findInHash(x);
            if (poz != -1) {//daca x se afla in hash stergem elementul de pe pozitia returnata de functie
                Hash[x % valueHash].erase(Hash[x % valueHash].begin() + poz);
            }
        } else { //verificam daca x este in multime
            out << (findInHash(x) == - 1? 0 : 1) << '\n';
        }
    }

    in.close();
    out.close();
    return 0;
}