Cod sursa(job #2682293)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 8 decembrie 2020 13:33:32
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <vector>


template <size_t MOD>
class HashSet {
private:
  std::vector<int> data[MOD];

  int getHashCode(int val) {
    return val % MOD;
  }

  std::pair<int, int> findPos(int val) {
    int hash = getHashCode(val);

    for (int i = 0; i < data[hash].size(); ++i) {
      if (data[hash][i] == val)
        return {hash, i};
    }

    return {hash, -1};
  }

public:
  HashSet() = default;

  bool find(int val) {
    auto pos = findPos(val);

    return pos.second != -1;
  }

  void remove(int val) {
    auto pos = findPos(val);

    if (pos.second == -1)
      return;

    data[pos.first][pos.second] = data[pos.first].back();
    data[pos.first].pop_back();
  }

  void insert(int val) {
    auto pos = findPos(val);

    if (pos.second != -1)
      return;

    data[pos.first].push_back(val);
  }
};

HashSet<100000> hs;

int main() {
  std::ifstream in("hashuri.in");
  std::ofstream out("hashuri.out");

  int n;
  in >> n;

  int op, a;

  for (int i = 1; i <= n; ++i) {
    in >> op >> a;

    if (op == 1)
      hs.insert(a);
    else if (op == 2)
      hs.remove(a);
    else
      out << hs.find(a) << '\n';
  }

  return 0;
}