Cod sursa(job #3353963)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 12 mai 2026 22:22:22
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>
using namespace std;

const int MOD = 666013;

struct Node {
    int x;
    Node* next;
    Node(int val) {
        x = val;
        next = NULL;
    }
};

Node* head[MOD];
Node* last[MOD];

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

void initHash() {
    for(int i = 0; i < MOD; i++) {
        head[i] = last[i] = NULL;
    }
}

bool search(Node* head, int x) {
    Node* p = head;
    while(p != NULL) {
        if(p -> x == x) {
            return true;
        }
        p = p -> next;
    }
    return false;
}

void add(Node* &head, Node* &last, int x) {
    Node* p = new Node(x);
    if(head == NULL) {
        head = last = p;
    } else {
        last -> next = p;
        last = p;
    }
}

void del(Node* &head, Node* &last, int x) {
    Node* p = head;
    Node* before = NULL;
    while(p != NULL && p -> x != x) {
        before = p;
        p = p -> next;
    }
    if(p == NULL) {
        return;
    }
    if(before == NULL) {
        head = head -> next;
        if(head == NULL) {
            last = NULL;
        }
        delete p;
    } else {
        before -> next = p -> next;
        if(p == last) {
            last = before;
        }
        delete p;
    }
}

int main() {
    ifstream cin("hashuri.in");
    ofstream cout("hashuri.out");
    initHash();
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int op, x;
        cin >> op >> x;
        int id = hashFunction(x);
        if(op == 1) {
            if(search(head[id], x) == false) {
                add(head[id], last[id], x);
            }
        }
        if(op == 2) {
            del(head[id], last[id], x);
        }
        if(op == 3) {
            cout << search(head[id], x) << '\n';
        }
    }
    return 0;
}