Cod sursa(job #2612395)

Utilizator dariahazaparuHazaparu Daria dariahazaparu Data 8 mai 2020 22:17:22
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <vector>

const int M = 666663;
std :: vector <int> hash[M];

bool cauta (int x) {
    int val = x % M;
    for (int i : hash[val]) {
        if (i == x) return true;
    }
    return false;
}

void adauga (int x) {
    int val = x % M;
    hash[val].push_back(x);
}

void sterge (int x) {
    int val = x % M;
    int size = hash[val].size();

    for (int i = 0; i < size; ++i) {
        if (hash[val][i] == x) {
            std :: swap(hash[val][i], hash[val][size-1]);
            break;
        }
    }
    hash[val].pop_back();
}

int main() {

    std :: ifstream fin ("hashuri.in");
    std :: ofstream fout ("hashuri.out");
    int n;
    std :: cin >> n;
    for (int i = 0; i < n; ++i) {
        int op, x;
        std :: cin >> op >> x;
        switch (op)
        {
            case 1:
                if (!cauta(x)) adauga(x);
                break;
            case 2:
                if (cauta(x)) sterge(x);
                break;
            case 3:
                std :: cout << cauta(x) << '\n';
        }
    }

    return 0;
}