Cod sursa(job #3246355)

Utilizator MateicostiGoidan Matei-Constantin Mateicosti Data 2 octombrie 2024 19:39:51
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
/*
 * Inlantuire directa
 * variem functiile de dispersie
 * sa facem multe teste
 */

/* Metode de testare:
 * 1) Generarea numerelor random pe interval;
 * 2) Putem genera teste adversariale pentru functile de Hash;
 * 3) Generare numere random si aleg cu +- 10^9 numarul in zona (Clustare);
 */

#include <fstream>
#include <iostream>
#include <list>
#include <vector>
#define p 1000003

std::ifstream fin("hashuri.in");
std::ofstream fout("hashuri.out");

const int NMAX = (int)1e6 + 1;
std::vector<std::list<int>> v;

int myHash(int x) { return x % p; }

bool inHash(int x) {
  for (auto a : v[myHash(x)]) {
    if (a == x) {
      return true;
    }
  }

  return false;
}

void insertHash(int x) {
  if (!inHash(x)) {
    v[myHash(x)].push_back(x);
  }
}

void removeHash(int x) {
  if (inHash(x)) {
    v[myHash(x)].remove(x);
  }
}

int main(int argc, char *argv[]) {
  if (!fin.is_open()) {
    std::cout << "Couldn't open file!\n";
    return 1;
  }

  int n, op, x;
  fin >> n;
  v.resize(p);

  for (int i = 0; i < n; i++) {
    fin >> op >> x;

    if (op == 1) {
      insertHash(x);
    } else if (op == 2) {
      removeHash(x);
    } else if (op == 3) {
      fout << inHash(x) << '\n';
    }
  }

  fin.close();
  fout.close();

  return 0;
}