Cod sursa(job #643084)

Utilizator ariel_roAriel Chelsau ariel_ro Data 2 decembrie 2011 21:42:47
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>

#define M 666667

using namespace std;

vector<int> hash[M];

int hashFunc(int x)
{
    return x % M;
}

vector<int>::iterator find(int x)
{
    int poz = hashFunc(x);
    vector<int>::iterator it;

    for (it = hash[poz].begin(); it != hash[poz].end(); ++it)
    {
        if (*it == x)
            return it;
    }
    return hash[poz].end();
}

void insert(int x)
{
    int poz = hashFunc(x);
    vector<int>::iterator it = find(x);
    if (it == hash[poz].end())
    {
        hash[poz].push_back(x);
    }
}

void del(int x)
{
    int poz = hashFunc(x);
    vector<int>::iterator it = find(x);
    if (it != hash[poz].end())
    {
        hash[poz].erase(it);
    }
}

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int N, op, x;
    for (scanf("%d", &N); N; --N)
    {
        scanf("%d %d", &op, &x);
        if (op == 1) // inserare
        {
            insert(x);
            continue;
        }
        if (op == 2) // stergere
        {
            del(x);
            continue;
        }
        printf("%d\n", find(x) != hash[hashFunc(x)].end());
    }
    return 0;
}