Cod sursa(job #2371202)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 6 martie 2019 16:40:16
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
#define N 123457

using namespace std;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");

int n;
vector <int> L[N];

/**
Toate functiile de hash intorc un numar intre 0 si
N-1, unde N este dimensiunea maxima a tabelei de hash.
Este recomandat ca N sa fie ales un numar
prim si sa se evite alegerea lui N = 2k.
*/

inline void Adauga(int x)
{
    int rest = x % N;
    L[rest].push_back(x);
}

inline void Sterge(int x)
{
    int rest, i, lung;
    rest = x % N;
    lung = L[rest].size();
    for (i = 0; i < lung; i++)
        if (L[rest][i] == x)
        {
            L[rest][i] = L[rest].back();
            L[rest].pop_back();
            return;
        }
}

inline bool Cauta(int x)
{
    int rest = x % N;
    for (auto w : L[rest])
        if (w == x)
            return 1;
    return 0;
}

int main()
{
    int i, op, x;
    fin >> n;
    for (i = 1; i <= n; i++)
    {
        fin >> op >> x;
        if (op == 1) Adauga(x);
        else if (op == 2) Sterge(x);
        else fout << Cauta(x) << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}