Pagini recente » Cod sursa (job #2795840) | Istoria paginii runda/serj/clasament | Istoria paginii runda/ce-o-sa-ma-injure-colegii/clasament | Cod sursa (job #190740) | Cod sursa (job #1970135)
#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, ¶m);
if(op == 1){
hash_insert(param);
} else if(op == 2){
hash_remove(param);
} else {
printf("%d\n", hash_find(param));
}
}
}