Pagini recente » Cod sursa (job #108931) | Cod sursa (job #1722167) | Cod sursa (job #1104084) | Cod sursa (job #2052804) | Cod sursa (job #316727)
Cod sursa(job #316727)
# include <algorithm>
using namespace std;
# define DIM 1 << 20
struct hash {
int val;
hash *urm;
};
int t, x;
hash *lst[DIM];
int check (int val) {
int poz;
hash *p;
poz = val % DIM;
for (p = lst[poz]; p; p = p->urm)
if(p->val == val)
return 1;
return 0;
}
/*
void add (int val) {
int poz;
poz = val % DIM;
if (check (val))
return;
hash *p = new hash;
p->val = val;
p->urm = lst[poz];
lst[poz] = p;
}
*/
int add(int a)
{
hash *current=lst[a];
while(current!=NULL)
{
if(current->val==x)
return 0;
current=current->urm;
}
hash *newh=new hash;
newh->val = x;
if(lst[a]==NULL)
{
lst[a]=newh;
newh->urm=NULL;
}
else
{
newh->urm=lst[a];
lst[a]=newh;
}
return 0;
}
/*
void del (int val) {
int poz;
hash *p, *q;
poz = val % DIM;
if (lst[poz] == NULL)
return;
if (lst[poz]->val == val){
p = lst[poz];
lst[poz] = p->urm;
delete p;
return;
}
q = NULL;
for (p = lst[poz]; p; q = p, p = p->urm)
if (p->val == val){
q->urm = p->urm;
delete p;
return;
}
}
*/
int del(int a)
{
hash *current=lst[a],*current2=lst[a];
if(lst[a]==NULL)
return 0;
if(current->val==x)
{
lst[a]=lst[a]->urm;
delete current;
return 0;
}
current=current->urm;
while(current!=NULL)
{
if(current->val==x)
{
current2->urm=current->urm;
delete current;
return 0;
}
current2=current;
current=current->urm;
}
return 0;
}
void solve () {
int i, /*x,*/ tip;
scanf ("%d", &t);
for (i = 0; i < t; ++ i){
scanf ("%d%d", &tip, &x);
if (tip == 1)
add (x % DIM);
else if (tip == 2)
del (x % DIM);
else if (tip == 3)
printf ("%d\n", check (x));
}
}
int main () {
freopen ("hashuri.in", "r", stdin);
freopen ("hashuri.out", "w", stdout);
solve ();
return 0;
}