Cod sursa(job #1566039)

Utilizator sherban26FMI Mateescu Serban-Corneliu sherban26 Data 11 ianuarie 2016 19:21:03
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <vector>

#define MOD 650359

using namespace std;

ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");

vector<long> bucket[MOD];

void hash_insert(long x);
void hash_delete(long x);
vector<long>::iterator hash_find(long x);

int main()
{
    int n, op;
    long x;

    for (fin >> n; n; --n)
    {
        fin >> op >> x;

        switch(op)
        {
        case 1:
            {
                hash_insert(x);
            } break;
        case 2:
            {
                hash_delete(x);
            } break;
        case 3:
            {
                if (hash_find(x) != bucket[x % MOD].end())
                    fout << "1\n";
                else
                    fout << "0\n";
            } break;
        default:
            { } break;
        }
    }
    return 0;
}

void hash_insert(long x)
{
    int index = x % MOD;

    if (hash_find(x) == bucket[index].end())
        bucket[index].push_back(x);
}

void hash_delete(long x)
{
    int index = x % MOD;

    vector<long>::iterator it = hash_find(x);

    if (hash_find(x) != bucket[index].end())
        bucket[index].erase(it);
}

vector<long>::iterator hash_find(long x)
{
    int index = x % MOD;

    for (vector<long>::iterator it = bucket[index].begin(); it != bucket[index].end(); it++)
        if (*it == x)
            return it;

    return bucket[index].end();
}