Cod sursa(job #1562484)

Utilizator geni950814Geni Geni geni950814 Data 5 ianuarie 2016 09:46:53
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <cstdio>
#include <vector>

using namespace std;

const int PRIME = 66013;

int hashMod(const int& n) {
    return n%PRIME;
}

int exist(vector<int>& list, const int& n) {
    int i = 0;
    for(int x : list) {
        if(x == n) {
            return i;
        }
        i++;
    }
    return -1;
}

void add(vector<vector<int>>& set, const int& n) {
    int key = hashMod(n);
    if(exist(set[key], n) == -1) {
        set[key].push_back(n);
    }
}

void remove(vector<vector<int>>& set, const int& n) {
    int key = hashMod(n);
    int index = exist(set[key], n);
    if(index != -1) {
        set[key].erase(set[key].begin()+index);
    }
}

int lookUp(vector<vector<int>>& set, const int& n) {
    int key = hashMod(n);
    if(exist(set[key], n) != -1) {
        return 1;
    }
    return 0;
}


int main() {
    int N;

    vector<vector<int>> set;

    for(int i = 0; i < PRIME; i++) {
        set.push_back(vector<int>(0));
    }

    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    scanf("%d", &N);

    int fst, snd;
    for(int i = 0; i < N; i++) {
        scanf("%d %d", &fst, &snd);
        switch(fst) {
            case 1:
                add(set, snd);
                break;

            case 2:
                remove(set, snd);
                break;

            case 3:
                printf("%d\n", lookUp(set, snd));
                break;

         }
    }
                
    return 0;
}