Cod sursa(job #644522)

Utilizator ariel_roAriel Chelsau ariel_ro Data 6 decembrie 2011 22:02:51
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <vector>

using namespace std;
#define M 800000

int N;
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);
    if (find(x) == 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()
{
    int op, x;

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

    scanf("%d", &N);
    while (N > 0)
    {
        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());
        }
        N--;
    }

    return 0;
}