Pagini recente » Cod sursa (job #1254583) | Istoria paginii runda/pre004 | Cod sursa (job #1621774) | Cod sursa (job #1320170) | Cod sursa (job #316729)
Cod sursa(job #316729)
/*
# include <algorithm>
using namespace std;
# define DIM 1 << 20
struct hash {
int val;
hash *urm;
};
int t;
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;
}
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;
}
}
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);
else if (tip == 2)
del (x);
else if (tip == 3)
printf ("%d\n", check (x));
}
}
int main () {
freopen ("hashuri.in", "r", stdin);
freopen ("hashuri.out", "w", stdout);
solve ();
return 0;
}
*/
#include <stdio.h>
#define nr 1234561
struct hash{
int info;
hash *next;
};
hash *p[nr];
int n,op,x;
int adauga(int a)
{
hash *current=p[a];
while(current!=NULL)
{
if(current->info==x)
return 0;
current=current->next;
}
hash *newh=new hash;
newh->info = x;
if(p[a]==NULL)
{
p[a]=newh;
newh->next=NULL;
}
else
{
newh->next=p[a];
p[a]=newh;
}
return 0;
}
int sterge(int a)
{
hash *current=p[a],*current2=p[a];
if(p[a]==NULL)
return 0;
if(current->info==x)
{
p[a]=p[a]->next;
delete current;
return 0;
}
current=current->next;
while(current!=NULL)
{
if(current->info==x)
{
current2->next=current->next;
delete current;
return 0;
}
current2=current;
current=current->next;
}
return 0;
}
int query(int a)
{
hash *current=p[a];
while(current!=NULL)
{
if(current->info==x)
return 1;
current=current->next;
}
return 0;
}
int main()
{
int i;
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d%d",&op,&x);
if(op==1)
adauga(x%nr);
if(op==2)
sterge(x%nr);
if(op==3)
printf("%d\n",query(x%nr));
}
return 0;
}