Cod sursa(job #1564168)

Utilizator M.AndreiMuntea Andrei Marius M.Andrei Data 8 ianuarie 2016 20:01:20
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream f("hashuri.in");
ofstream q("hashuri.out");

const int modulo = 666013;

int n,op,el;
vector <int> hashTable[modulo];

void adauga(int el);
int gaseste(int el);
void sterge(int el);

int main()
{
    f>>n;
    for ( ; n > 0; n--)
    {
        f >> op >> el;
        if (op == 1) adauga(el);
        else if (op==2) sterge(el);
        else if (gaseste(el)==-1) q<<0<<"\n";
        else q<<1<<"\n";
    }
    f.close();
    q.close();
    return 0;
}

int gaseste(int el)
{
    int poz = 0, rez = el%modulo;
    vector <int>::iterator it;
    for(it = hashTable[rez].begin(); it != hashTable[rez].end(); ++it)
    {
        if (*it == el)
        {
            return poz;
        }
        poz++;
    }
    return -1;
}

void adauga(int el)
{
   if (gaseste(el) == -1)
   {
       hashTable[el % modulo].push_back(el);
   }
}

void sterge(int el)
{
    int poz = gaseste(el);
    if ( poz != -1 ) hashTable[el % modulo].erase(hashTable[el % modulo].begin() + poz);
}