Cod sursa(job #919542)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 19 martie 2013 18:39:34
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <cstdio>
 
#include <list>
 
using namespace std;
 
const int P = 599993;
 
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;
}