Cod sursa(job #2761560)

Utilizator MihaiBirsanMihai Birsan MihaiBirsan Data 2 iulie 2021 17:47:24
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

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

#define nrprim 79657

vector<int> v[nrprim];
int n, cmd, x;

int exista(int x)
{
    int hsh = x % nrprim;
    int l = v[hsh].size();
    for(int i = 0; i < l; i++)
    {
        if(v[hsh][i] == x)
            return 1;
    }
    return 0;
}

void adauga(int x)
{
    int hsh = x % nrprim;
    if(exista(x) == 0)
    {
        v[hsh].push_back(x);
    }
}

void sterge(int x)
{
    int hsh = x % nrprim;
    int l = v[hsh].size();
    for(auto i = v[hsh].begin(); i != v[hsh].end(); i++)
    {
        if( x == *i)
        {
            v[hsh].erase(i);
            break;
        }

    }

}


int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> cmd >> x;
        if(cmd == 1)
        {
            adauga(x);
        }
        if(cmd == 2)
        {
            sterge(x);
        }
        if(cmd == 3)
        {
            fout << exista(x) << '\n';
        }
    }

    return 0;
}