Cod sursa(job #2292584)

Utilizator mihaicivMihai Vlad mihaiciv Data 29 noiembrie 2018 18:35:43
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

const int MOD = 196613;

vector <int> a[MOD];

void add(int);
void eraseElement(int);
int isElement(int);

int main() {

    ifstream f("hashuri.in");
    ofstream g("hashuri.out");

    int query;
    f >> query;

    while (query--) {

        int type, x;
        f >> type >> x;
        if (type == 1) { add(x);}
        if (type == 2) { eraseElement(x);}
        if (type == 3) { g << isElement(x) << "\n";}
    }

    return 0;
}

void add(int x) {
    int key = x % MOD;

    bool found = false;

    for (int i = 0; i < a[key].size(); ++i) {
        if (a[key][i] == x) { found = true;}
        else if (a[key][i] == -1) {
            found = true;
            a[key][i] = x;
        }
    }

    if ( !found ) {
        a[key].push_back(x);
    }
}

void eraseElement(int x) {

    int key = x % MOD;

    bool found = false;

    for (int i = 0; i < a[key].size(); ++i) {
        if (a[key][i] == x) {
            found = true;
            a[key][i] = -1; /// marchez ca sters
        }
    }
}

int isElement(int x) {

    int key = x % MOD;


    int found = 0;

    for (int i = 0; i < a[key].size() && found == 0; ++i) {
        if (a[key][i] == x) {
            found = 1;
        }
    }
    return found;
}