Pagini recente » Cod sursa (job #16974) | Cod sursa (job #1672453) | Cod sursa (job #274600) | Cod sursa (job #2683600) | Cod sursa (job #2741930)
#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;
}
}
}
}