Cod sursa(job #2736445)

Utilizator blxqnAlina Voiculescu blxqn Data 3 aprilie 2021 14:42:56
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#define maxx 1000005

using namespace std;

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

const int prime = 9973;
int val[maxx], urm[maxx], lst[prime], nr;

void add(int x)
{
    int bucket = x % prime;
    nr += 1;
    val[nr] = x;
    urm[nr] = lst[bucket];
    lst[bucket] = nr;
}

bool searchh(int x)
{
    int bucket = x % prime;
    int p = lst[bucket];
    while(p != 0 && val[p] != x)
        p = urm[p];
    if (p)
        return true;
    return false;

}

void remove(int x)
{
    int bucket = x % prime;
    int p = lst[bucket];
    while(p != 0 && val[p] != x)
        p = urm[p];
    if (p != 0)
    {
        swap(val[p], val[lst[bucket]]);
        lst[bucket] = urm[lst[bucket]];

    }
}

int main()
{
    int operatii;
    fin >> operatii;
    while(operatii--)
    {
        int tip_operatie, val;
        fin >> tip_operatie >> val;
        if(tip_operatie == 1)
            add(val);
        if(tip_operatie == 2)
            remove(val);
        if(tip_operatie == 3)
            fout << searchh(val) << '\n';
    }
    return 0;
}