Cod sursa(job #907519)

Utilizator razvan9310FMI - Razvan Damachi razvan9310 Data 7 martie 2013 23:50:51
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.24 kb
#include <cstdio>
#include <cstdlib>
#include <ctime>
#define prim 1000000009
 
class Cuckoo
{
    private:
        int a1, a2, b1, b2;
        int size, *H1, *H2;
         
    public:
        Cuckoo(int SIZE)
        {
            srand(time(NULL));
            size = SIZE;
            H1 = (int*)calloc(size, sizeof(int));
            H2 = (int*)calloc(size, sizeof(int));
            a1=rand()%size; a2=rand()%size;
            b1=rand()%size; b2=rand()%size;
        }           
         
        int hash1(int &x)
        {
            return (((long long)a1*x + b1)%prim)%size;
        }
         
        int hash2(int &x)
        {
            return (((long long)a2*x + b2)%prim)%size;
        }
         
        void rehash()
        {
            a1=rand()%size; a2=rand()%size;
            b1=rand()%size; b2=rand()%size;
             
            int *H1_temp = H1;
            int *H2_temp = H2;
            H1 = (int*)calloc(size, sizeof(int));
            H2 = (int*)calloc(size, sizeof(int));
             
            for (int i=0; i<size; ++i)
            {
                if (H1_temp[i]) insert(H1_temp[i]);
                if (H2_temp[i]) insert(H2_temp[i]);
            }
             
            free(H1_temp);
            free(H2_temp);              
        }
         
        bool find(int &x)
        {
            return (H1[hash1(x)]==x || H2[hash2(x)]==x);
        }
         
        bool insert(int x)
        {
            if (find(x))
                return 0;
            int aux, val1, val2, i, old=x;
            bool inPlace = 0;
            for (i=0; !inPlace; ++i)
            {
                if (i && x==old)
                    rehash();
                val1=hash1(x); val2=hash2(x);
                if (H1[val1] == 0)
                {
                    H1[val1] = x;
                    inPlace = 1;
                }
                else if (H2[val2] == 0)
                {
                    H2[val2] = x;
                    inPlace = 1;
                }
                else
                {
                    aux = H1[val1];
                    H1[val1]=x; x=aux;
                    val2 = hash2(x);
                    if (H2[val2] == 0)
                    {
                        H2[val2] = x;
                        inPlace = 1;
                    }
                    else
                    {
                        aux = H2[val2];
                        H2[val2]=x; x=aux;
                    }
                }
            }
            return 1;
        }
         
        bool erase(int &x)
        {
            if (!find(x)) return 0;
            if (H1[hash1(x)] == x)
                H1[hash1(x)] = 0;
            else H2[hash2(x)] = 0;
            return 1;
        }
};
 
int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    int n, c, x, i;
    Cuckoo hash(1000000);
    scanf("%d", &n);
    for (i=0; i<n; ++i)
    {
        scanf("%d%d", &c, &x);
        switch (c)
        {
            case 1: hash.insert(x); break;
            case 2: hash.erase(x); break;
            case 3: printf("%d\n", hash.find(x)); break;
        }
    }
    return 0;
}