Cod sursa(job #496063)

Utilizator cristiprgPrigoana Cristian cristiprg Data 27 octombrie 2010 18:13:59
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define MOD 666667
#define pb push_back
#define IT vector<int>::iterator

vector<int>A[MOD];

inline int H(int i)
{
    return i%MOD;
}

IT search(int x)
{
    for (IT it = A[H(x)].begin(); it < A[H(x)].end(); ++it)
        if (x == *it)
            return it;
    return A[H(x)].end();
}

bool exista(int x)
{
    if (search(x) != A[H(x)].end())
        return true;
    return false;
}

void add(int x)
{
    A[H(x)].pb(x);
}

void remove(int x)
{
    IT it = search(x);
    if (it != A[H(x)].end())
        *it = 0;
}

int main()
{
    FILE *f = fopen("hashuri.in", "r"), *out = fopen("hashuri.out", "w");
    int n, type, x;


    for(fscanf(f, "%d", &n);n; --n)
    {
        fscanf(f, "%d%d", &type, &x);
        if (type == 1)
            if (!exista(x))
                add(x);

        if (type == 2)
            remove(x);



        if (type == 3)
            if (exista(x))
                fprintf(out, "1\n");
            else
                fprintf(out, "0\n");

    }

    fclose(f);
    fclose(out);
    return 0;
}