Cod sursa(job #3131009)

Utilizator stefanmo03Mocanu Stefan stefanmo03 Data 19 mai 2023 00:07:52
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
//#include <iostream>
#include <vector>
#include <fstream>
//using namespace std;
std::ifstream cin("hashuri.in");
std::ofstream cout("hashuri.out");
template<const int size>
class Hash{
    std::vector<long long> vals;
    std::vector<long long> next;
    std::vector<long long> start;

public:
    Hash(){vals.resize(1);
        next.resize(1);
        start.resize(size);

    }
    void add(long long val){
        long long h = val % size;

        vals.push_back(val);
        next.push_back(start[h]);
        start[h] = vals.size() -1;
    }
    void erase(long long val){
        long long h = val % size;
        long long poz = start[h];
        if(vals[poz] == val){
            start[h] = 0;

            return;
        }
        while(next[poz]){
            if(vals[next[poz]]==val){
               
                next[poz] = next[next[poz]];
                break;
            }
            poz = next[poz];
        }
    }
    bool find(long long val){
        long long h = val % size;
        long long poz = start[h];
        while(poz){
            if(vals[poz]==val){
                return true;
            }
            poz = next[poz];
        }
        return false;
    }
};

int main() {
    long long n;
    cin>>n;
    Hash<666013> hash;
    for(long long i=0;i<n;i++){
        long long comanda, nr;
        cin>>comanda>>nr;
        switch (comanda) {
            case 1:{hash.add(nr);break;}
            case 2:{hash.erase(nr);break;}
            case 3:{cout<<hash.find(nr)<<'\n';break;}
            default:{}
        }
    }
    return 0;
}