Cod sursa(job #2821783)

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

using namespace std;

class HashMap
{
public:

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

    int poz = this->Find(x);

    if (poz == -1)
      this->resturi[Hash].emplace_back(x, 1);
    else
      this->resturi[Hash][poz].second++;
  }

  int 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 i;

    return -1;
  }

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

    int poz = this->Find(x);

    if (poz == -1)
      return;

    this->resturi[Hash][poz].second--;

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

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) != -1)
        out << 1 << '\n';
      else
        out << 0 << '\n';
    }
  }

  return 0;
}