Cod sursa(job #3378964)

Utilizator TimofeiFilipTimofei Filip Emanuel TimofeiFilip Data 28 noiembrie 2026 22:16:48
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb

#include <bits/stdc++.h>
using namespace std;

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

const int NMAX = 2e6 + 5;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;

const int EMPTY = 0;
const int DELETED = -1;
const int OCCUPIED = 1;

struct Hash {
    int value;
    int status;
};

Hash arr[NMAX];

int hash1(int x) {
    return x % MOD;
}

int hash2(int x) {
    return x % MOD2;
}

int find(int x) {
    int h1 = hash1(x);
    int h2 = hash2(x);

    for (int i = 0; i < NMAX; i++) {
        int pos = (h1 + 1LL * i * h2) % NMAX;

        if (arr[pos].status == EMPTY)
            return -1;

        if (arr[pos].status == OCCUPIED &&
            arr[pos].value == x)
            return pos;
    }

    return -1;
}

void insert(int x) {
    if (find(x) != -1)
        return;

    int h1 = hash1(x);
    int h2 = hash2(x);

    for (int i = 0; i < NMAX; i++) {
        int pos = (h1 + 1LL * i * h2) % NMAX;

        if (arr[pos].status != OCCUPIED) {
            arr[pos].value = x;
            arr[pos].status = OCCUPIED;
            return;
        }
    }
}

void erase(int x) {
    int pos = find(x);

    if (pos != -1)
        arr[pos].status = DELETED;
}

int main() {
    int N;
    fin >> N;

    while (N--) {
        int op, x;
        fin >> op >> x;

        if (op == 1)
            insert(x);
        else if (op == 2)
            erase(x);
        else
            fout << (find(x) != -1) << '\n';
    }

    return 0;
}