Cod sursa(job #1438219)

Utilizator R4DIC4LTeodorescu Oana Maria R4DIC4L Data 19 mai 2015 13:17:17
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#include <vector>
#define MOD 666013
using namespace std;
 
fstream in("hashuri.in");
ofstream out("hashuri.out");
 
int n, op, val;
vector<int> h[MOD];
 
vector<int>::iterator find_it(int key, int value)
{
    for (auto it = h[key].begin(); it != h[key].end(); ++it)
    {
        if (*it == value)
        {
            return it;
        }
    }
 
    return h[key].end();
}
 
bool find(int value)
{
    int key = value % MOD;
 
    return find_it(key, value) != h[key].end();
}
 
void insert(int value)
{
    int key = value % MOD;
 
    if (!find(value))
    {
        h[key].push_back(value);
    }
}
 
void remove(int value)
{
    int key = value % MOD;
 
    auto pos = find_it(key, value);
    if (pos != h[key].end())
    {
        h[key].erase(pos);
    }
}
 
int main()
{
    in >> n;
    while (n--)
    {
        in >> op >> val;
        if (op == 1)
        {
            insert(val);
        }
        else
        {
            if (op == 2)
            {
                remove(val);
            }
            else
            {
                out << (find(val) ? 1 : 0) << "\n";
            }
        }
    }
    in.close();
    out.close();
    return 0;
}