Cod sursa(job #2893627)

Utilizator mihairazvan03Dana Mihai mihairazvan03 Data 26 aprilie 2022 14:26:55
Problema Hashuri Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <vector>
#include <fstream>

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

int n, p = 666015, i, x, op, hashedX, ok, j;
vector<vector<int>> hashMatrix;

int main()
{
    hashMatrix.resize(p + 2); //avem cate o lista pentru fiecare rest de la 0 la p - 1
    in>>n;

    for(i = 1; i <= n; i++)
    {
        in>>op>>x;
        hashedX = x % p;

        if(op == 1)
        {
            ok = 0;
            for(j = 0; j < hashMatrix[hashedX].size(); j++)
                if(hashMatrix[hashedX][j] == x)
                {
                    ok = 1;
                    break;
                }

            if(ok == 0)
                hashMatrix[hashedX].push_back(x);
        }
        else if(op == 2)
        {
            for(j = 0; j < hashMatrix[hashedX].size(); j++)
                if(hashMatrix[hashedX][j] == x)
                {
                    hashMatrix[hashedX][j] = hashMatrix[hashedX].back();
                    hashMatrix[hashedX].pop_back();
                    break;
                }
        }
        else
        {
            ok = 0;
            for(j = 0; j < hashMatrix[hashedX].size(); j++)
                if(hashMatrix[hashedX][j] == x)
                {
                    ok = 1;
                    break;
                }

            out<<ok<<endl;
        }
    }

    return 0;
}