Cod sursa(job #2741930)

Utilizator icnsrNicula Ionut icnsr Data 19 aprilie 2021 19:27:08
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.16 kb
#include <fstream>
#include <array>
#include <vector>
#include <algorithm>

#define NOINLINE __attribute__((noinline))
 
class USet
{
public:
        NOINLINE void insert(const unsigned key)
        {
                const auto hash = get_hash(key);
 
                auto& tvec = buckets[hash];
                if(std::find(tvec.begin(), tvec.end(), key) == tvec.end())
                {
                        tvec.push_back(key);
                }
        }
 
        NOINLINE void erase(const unsigned key)
        {
                const auto hash = get_hash(key);
 
                auto& tvec = buckets[hash];
                const auto it = std::find(tvec.begin(), tvec.end(), key);
                if(it != tvec.end())
                {
                        tvec.erase(it);
                }
        }
 
        NOINLINE bool contains(const unsigned key)
        {
                const auto hash = get_hash(key);
 
                auto& tvec = buckets[hash];
                return std::find(tvec.begin(), tvec.end(), key) != tvec.end();
        }
 
        static constexpr unsigned SIZE = 49157;
 
private:
        unsigned get_hash(const unsigned key) const
        {
                return key % SIZE;
        }
 
private:
        std::array<std::vector<unsigned>, SIZE> buckets;
};
 
int main()
{
        std::ifstream fin("hashuri.in");
        std::ofstream fout("hashuri.out");
        unsigned N;
        fin >> N;
 
        USet uset;
        for(unsigned i = 0; i < N; ++i)
        {
                int op;
                unsigned key;
                fin >> op >> key;
 
                switch(op)
                {
                        case 1:
                        {
                                uset.insert(key);
                                break;
                        }
                        case 2:
                        {
                                uset.erase(key);
                                break;
                        }
                        case 3:
                        {
                                fout << uset.contains(key) << '\n';
                                break;
                        }
                }
        }
}