Pagini recente » Cod sursa (job #1393584) | Cod sursa (job #2589813) | Cod sursa (job #1503306) | Cod sursa (job #1979003) | Cod sursa (job #922295)
Cod sursa(job #922295)
#include <cstdio>
#include <tr1/functional>
#define MOD 2097151
template <class tip>
class Hash
{
public:
Hash() {
hh = new tip[MOD+1]();
ok = new char[MOD + 1]();
}
~Hash() {delete[] hh;}
//inline Hash& operator += (int&);
//inline Hash& operator -= (int&);
inline Hash& operator+=(const tip &val)
{
insert(val);
return *this;
}
inline Hash& operator-=(const tip &val)
{
del(val);
return *this;
}
inline int newpoz(int poz)
{
return (poz*5+1)&MOD;
}
inline int insert(const tip &x)
{
int poz=hashff(x) & MOD ,steps=0;
while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (steps>=MOD)
return 0;
hh[poz]=x;
ok[poz]=1;
return poz;
}
inline int del(const tip &x)
{
int poz=hashff(x) & MOD,steps=0;
while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (steps>=MOD)
return 0;
if (hh[poz]!=x) return -1;
ok[poz]=-1;
return poz;
}
inline int getpoz(const tip &x)
{
int poz=hashff(x) & MOD, steps = 0;
while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
poz=newpoz(poz),++steps;
if (hh[poz]!=x) return 0;
return poz;
}
private:
tip *hh;
char *ok;
std::tr1::hash<tip> hashff;
};
int main()
{
int t,x,tip;
Hash<int> hass;
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
scanf("%d",&t);
while (t--)
{
scanf("%d %d",&tip,&x);
if (tip==1)
hass+=x;
else
if (tip==2) hass-=x; else
printf("%d\n",hass.getpoz(x)>0);
}
return 0;
}