Pagini recente » Cod sursa (job #1247273) | Cod sursa (job #1475731) | Cod sursa (job #3004828) | Cod sursa (job #1326952) | Cod sursa (job #921430)
Cod sursa(job #921430)
#include <cstdio>
#define MOD 2097151
class hash
{
public:
hash(int N) {
hh = new int[N+1];
}
~hash() {delete[] hh;}
inline int newpoz(int);
inline int insert(int&);
inline int del(int&);
inline int getpoz(int);
inline int operator += (int&);
inline int operator -= (int&);
private:
int *hh;
};
inline int hash::operator+=(int &val)
{
return insert(val);
}
inline int hash::operator-=(int &val)
{
return del(val);
}
inline int hash::newpoz(int poz)
{
return (poz*5+1)&MOD;
}
inline int hash::insert(int &x)
{
int poz=x & MOD,steps=0;
while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (steps>=MOD)
return 0;
hh[poz]=x;
return poz;
}
inline int hash::del(int &x)
{
int poz=x & MOD,steps=0;
while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (steps>=MOD)
return 0;
if (hh[poz]!=x) return -1;
hh[poz]=-1;
return poz;
}
inline int hash::getpoz(int x)
{
int poz=x & MOD, steps = 0;
while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (hh[poz]!=x) return 0;
return poz;
}
int main()
{
int t,x,tip;
hash has(MOD+1);
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
scanf("%d",&t);
while (t--)
{
scanf("%d %d",&tip,&x);
if (tip==1) has+=x; else
if (tip==2) has.del(x); else
printf("%d\n",has.getpoz(x)>0);
}
return 0;
}