Cod sursa(job #644530)

Utilizator ariel_roAriel Chelsau ariel_ro Data 6 decembrie 2011 22:16:36
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <vector>

#define M 666667

using namespace std;

vector<int> hash[M];

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

inline 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();
}

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

inline 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);
        }
        else if (op == 2) // stergere
        {
            del(x);
        }
        else if (op == 3)
        {
            printf("%d\n", find(x) != hash[hashFunc(x)].end());
        }

    }
    return 0;
}