Cod sursa(job #1492244)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 27 septembrie 2015 14:11:49
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.06 kb
#include <bits/stdc++.h>

const int Nmax = 1000000;
const int NIL = -1;
const int BUFFSIZE = 4096;

struct h
{
    int v, next;
};

h l[Nmax];
int lsize;
int h[65536];
int st[Nmax], s;
char buff[BUFFSIZE];
int ptr = BUFFSIZE;

inline int H(const int &x)
{
     return (((unsigned) x * 40503) & 65535);
}

inline bool hashFind(const int &x)
{
    int i = h[H(x)];

    while ((i != NIL) && (l[i].v != x))
        i = l[i].next;
    return (i != NIL);
}

inline void hashInsert(const int &x)
{
    int p = s ? st[--s] : lsize++;
    int hash = H(x);

    l[p].v = x;
    l[p].next = h[hash];
    h[hash] = p;
}

inline void hashExtract(const int &x)
{
    int hash = H(x);
    int i = h[hash];

    if (l[i].v == x)
        h[hash] = l[i].next;
    else
    {
        while ((l[i].next != NIL) && (l[l[i].next].v != x))
            i = l[i].next;
        if (l[i].next != NIL)
        {
            st[s++] = l[i].next;
            l[i].next = l[l[i].next].next;
        }
    }
}

inline char getChr()
{
    if (ptr == BUFFSIZE)
    {
        fread(buff, 1, BUFFSIZE, stdin);
        ptr = 0;
    }
    return buff[ ptr++ ];
}

inline int readInt()
{
    register int q = 0;
    char c;

    do
    {
        c = getChr();
    } while (!isdigit(c));
    do
    {
        q = (q << 1) + (q << 3) + (c - '0');
        c = getChr();
    } while (isdigit(c));
    return q;
}

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    int testNum;
    char opType;
    int x;

    memset(h, NIL, sizeof(h));

    testNum = readInt();
    for (; testNum; testNum--)
    {
        do
        {
            opType = getChr();
        } while (!isdigit(opType));
        x = readInt();
        if (opType == '1')
            hashInsert(x);
        else if (opType == '2')
            hashExtract(x);
        else
        {
            putchar(hashFind(x) + '0');
            putchar('\n');
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}