Cod sursa(job #922295)

Utilizator aladinaladin aladinn aladin Data 22 martie 2013 02:11:29
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.89 kb
#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;
}