Cod sursa(job #1970135)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 18 aprilie 2017 22:05:46
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000001;
const int MOD = 666013;

struct node{
    int value;
    node* next;
}*h[MOD];

void list_insert(node*& p, int val){
    if(!p){
        p = new node{val, NULL};
        return;
    }
    node* x = new node{val, NULL};
    p->next = x;
    p = x;
}
bool list_find(node*&p, int val){
    for(node* i = p; i; i = i->next)
        if(i->value == val){
            return 1;
        }
    return 0;
}

bool hash_find(int nod){
    int where = nod % MOD;
    return list_find(h[where], nod);
}

void hash_insert(int nod){
    int where = nod % MOD;
    if(!hash_find(nod)){
        list_insert(h[where], nod);
    }
}

void hash_remove(int nod){
    int where = nod % MOD;
    if(!hash_find(nod))
        return;
    if(h[where]->value == nod){
        if(!h[where]->next){
            delete h[where];
            h[where] = NULL;
        } else {
            node* aux = h[where];
            h[where]->next = h[where]->next->next;
            delete aux;
        }
        return;
    }
    for(node *i = h[where]; i->next ;i = i->next){
        if(i->next->value == nod){
            if(!i->next->next){
                delete i->next;
                i->next = NULL;
            } else {
                node *p = i->next;
                i->next = i->next->next;
                delete p;
            }
            return;
        }
    } 
}

int q;

int main(){
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    scanf("%d ", &q);
    while(q--){
        int op,  param;
        scanf("%d %d", &op, &param);
        if(op == 1){
            hash_insert(param);   
        } else if(op == 2){
            hash_remove(param);
        } else {
           printf("%d\n", hash_find(param)); 
        }
    }
}