Cod sursa(job #1968649)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 17 aprilie 2017 19:44:07
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;

const int mod = (1 << 16) - 1;

int n;
vector < int > h[mod+1];

int nxt(int x) {
    return (x * 17 + 1) & mod;
}

int _find(int x) {
    int whr = ((x & mod) * 17) & mod;
    for (int pos = 0; pos < h[whr].size(); ++pos)
        if (h[whr][pos] == x) return pos;
    return -1;
}

void _add(int x) {
    int whr = ((x & mod) * 17) & mod;

    int pos = _find(x);
    if (pos != -1) return;

    h[whr].push_back(x);
}

void _delete(int x) {
    int whr = ((x & mod) * 17) & mod;

    int pos = _find(x);
    if (pos == -1) return;

    h[whr][pos] = h[whr].back(); h[whr].pop_back();
}

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

    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int type, x;
        scanf("%d %d", &type, &x);

        if (type == 1)
            _add(x);
        if (type == 2)
            _delete(x);
        if (type == 3)
            printf("%d\n", (_find(x) != -1));
    }

    return 0;
}