Cod sursa(job #1017872)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 28 octombrie 2013 16:16:11
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream cin("hashuri.in");
ofstream cout("hashuri.out");

vector<int> hashu[666013];

int hashFunc (int nr)
{
  return nr % 666013;
}

int cauta(int n);

void adaugare (int val)
{
  // <----- ce tampenie ------>
  if (cauta(val) == 1)
    return;

  hashu[hashFunc(val)].push_back(val);
}

void stergere (int val)
{
  int hashed = hashFunc(val);

  for (vector<int>::iterator i = hashu[hashed].begin(); i != hashu[hashed].end(); i++)
    if (*i == val)
      {
	hashu[hashed].erase(i);
	return;
      }
}

int cauta(int val)
{
  int hashed = hashFunc(val);
  for (vector<int>::iterator i = hashu[hashed].begin(); i != hashu[hashed].end(); i++)
    if (*i == val)
      return 1;

  return 0;
}

int main()
{
  int n; cin >> n;
  for (int i = 1; i <= n; i++)
    {
      int tipOp, val;
      cin >> tipOp >> val;

      if (tipOp == 1)
	adaugare (val);
      if (tipOp == 2)
	stergere (val);
      if (tipOp == 3)
	cout << cauta(val) << endl;
    }

  cin.close();
  cout.close();

  return 0;
}