Cod sursa(job #919265)

Utilizator sebii_cSebastian Claici sebii_c Data 19 martie 2013 15:45:01
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>

#include <list>

using namespace std;

const int P = 799993;

list<int> hash_set[P];

inline int hash(int n)
{
    return n % P;
}

bool contains(int n)
{
    int h = hash(n);

    list<int>::iterator it;
    for (it = hash_set[h].begin(); it != hash_set[h].end(); ++it)
        if (*it == n)
            return true;

    return false;
}

void remove(int n)
{
    int h = hash(n);

    list<int>::iterator it;
    for (it = hash_set[h].begin(); it != hash_set[h].end(); ++it)
        if (*it == n) {
            hash_set[h].erase(it);
            break;
        }
}

void add(int n)
{
    int h = hash(n);

    list<int>::iterator it;
    for (it = hash_set[h].begin(); it != hash_set[h].end(); ++it)
        if (*it == n)
            return;

    hash_set[h].push_back(n);
}

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

    int N;
    scanf("%d", &N);

    for (int i = 0; i < N; ++i) {
        int op, x;
        scanf("%d %d", &op, &x);

        if (op == 1) {
            add(x);
        } else if (op == 2) {
            remove(x);
        } else {
            printf("%d\n", contains(x));
        }
    }
    
    return 0;
}