Cod sursa(job #1017874)

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

vector<int> hashu[886381];

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

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()
{
  freopen ("hashuri.in", "r", stdin);
  freopen ("hashuri.out", "w", stdout);
  
  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;
    }

  return 0;
}