Cod sursa(job #2650680)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 19 septembrie 2020 18:28:06
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <vector>

using namespace std;

const int MODULO = 666013;

class HashMap
{

public:
  HashMap() = default;

  void add(int key, int value)
  {
    auto pos = m_find(key);

    if (pos.second == -1)
    {
      hash_map[pos.first].push_back(make_pair(key, value));
    }
    else
    {
      hash_map[pos.first][pos.second].second = value;
    }
  }

  void remove(int key)
  {
    auto pos = m_find(key);

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

    hash_map[pos.first][pos.second] = hash_map[pos.first][hash_map[pos.first].size() - 1];
    hash_map[pos.first].pop_back();
  }

  int get(int key)
  {
    auto pos = m_find(key);

    if (pos.second == -1)
    {
      return -1;
    }
    else
    {
      return hash_map[pos.first][pos.second].second;
    }
  }

private:
  vector<pair<int, int>> hash_map[MODULO];

  pair<int, int> m_find(int key)
  {
    int hashed_key = key % MODULO;

    for (int i = 0; i < hash_map[hashed_key].size(); i++)
    {
      if (hash_map[hashed_key][i].first == key)
      {
        return make_pair(hashed_key, i);
      }
    }

    return make_pair(hashed_key, -1);
  }
};

HashMap hm;

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

  int n, op, value;

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

    if (op == 1)
    {
      hm.add(value, 1);
    }
    else if (op == 2)
    {
      hm.remove(value);
    }
    else
    {
      if (hm.get(value) == 1)
      {
        out << 1 << '\n';
      }
      else if (hm.get(value) == -1)
      {
        out << 0 << '\n';
      }
    }
  }

  return 0;
}