Cod sursa(job #2821779)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 23 decembrie 2021 01:07:12
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <vector>

using namespace std;

class HashMap
{
public:

  void Insert(int x)
  {
    int Hash = x % this->MODULO;

    if (!this->Find(x))
      this->resturi[Hash].emplace_back(x, 1);
  }

  bool Find(int x)
  {
    int Hash = x % this->MODULO;

    for (int i = 0; i < this->resturi[Hash].size(); i++)
      if (this->resturi[Hash][i].first == x)
        return true;

    return false;
  }

  void Erase(int x)
  {
    int Hash = x % this->MODULO;

    for (int i = 0; i < this->resturi[Hash].size(); i++)
    {
      if (this->resturi[Hash][i].first == x)
      {
        this->resturi[Hash][i].second--;

        if (this->resturi[Hash][i].second == 0)
        {
          this->resturi[Hash][i] = this->resturi[Hash].back();
          this->resturi[Hash].pop_back();
        }

        return;
      }
    }
  }

private:

  static const int MODULO = 666013;
  vector<pair<int, int>> resturi[MODULO];

};

HashMap hm;

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

  int n;
  in >> n;

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

    if (op == 1)
      hm.Insert(x);
    else if (op == 2)
      hm.Erase(x);
    else
    {
      if (hm.Find(x))
        out << 1 << '\n';
      else
        out << 0 << '\n';
    }
  }

  return 0;
}