Cod sursa(job #2153759)

Utilizator CammieCamelia Lazar Cammie Data 6 martie 2018 13:57:14
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>

#define mod1 666013

using namespace std;

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

vector <int> hashh[mod1 + 10];

inline void Add(int x) {
    int val = x % mod1;
    hashh[val].push_back(x);
}

inline void Delete(int x) {
    int val = x % mod1;
    int l = hashh[val].size() - 1, i = 0;

    while (i <= l) {
        if (hashh[val][i] == x) {
            swap(hashh[val][l], hashh[val][i]);
            hashh[val].pop_back();
        }
        i++;
    }
}

inline int Exista(int x) {
    int val = x % mod1;

    for (auto xx : hashh[val]) {
        if (xx == x)
            return 1;
    }
    return 0;
}

inline void Read() {
    int M, tip, x;

    fin >> M;

    while (M--) {
        fin >> tip >> x;

        if (tip == 1) {
            if (Exista(x) == 0) {
                Add(x);
            }
        }
        else if (tip == 2) {
            if (Exista(x))
                Delete(x);
        }
        else {
            fout << Exista(x) << "\n";
        }
    }
}

int main () {
    Read();

    fin.close(); fout.close(); return 0;
}