Cod sursa(job #2487345)

Utilizator SurduTonySurdu Tony SurduTony Data 4 noiembrie 2019 16:42:22
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <vector>
#include <fstream>

#define MOD 600000
using namespace std;

vector<int> hashTable[MOD];

vector<int>::iterator findValue(int value) {
    int poz = value % MOD;
    vector<int>::iterator i;

    for(i=hashTable[poz].begin(); i<hashTable[poz].end(); i++)
        if(*i == value) return i;

    return hashTable[poz].end();
}

void insertValue(int value) {
    int poz = value % MOD;

    if(findValue(value) == hashTable[poz].end())
        hashTable[poz].push_back(value);
}

void eraseValue(int value) {
    int poz = value % MOD;
    vector<int>::iterator i = findValue(value);

    if(i != hashTable[poz].end())
        hashTable[poz].erase(i);
}

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

    int N, op, value, index;

    fin >> N;

    for(int i=1; i<=N; i++) {
        fin >> op >> value;

        if(op == 1) {
            insertValue(value);
        }
        else if(op == 2) {
            eraseValue(value);
        }
        else {
            if(findValue(value) != hashTable[value%MOD].end())
                fout << 1 << '\n';
            else fout << 0 << '\n';
        }
    }

    return 0;
}