Cod sursa(job #1577185)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 23 ianuarie 2016 12:07:57
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 MOD = 666013;
const int NIL = -1;
const int BUFFSIZE = 4096;
 
struct h
{
    int v, next;
};
 
h l[Nmax];
int lsize;
int h[MOD];
int st[Nmax], s;
char buff[BUFFSIZE];
int ptr = BUFFSIZE;
 
inline bool hashFind(const int &x)
{
    int i = h[x - (x / MOD) * MOD];
 
    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 = x - (x / MOD) * MOD;
 
    l[p].v = x;
    l[p].next = h[hash];
    h[hash] = p;
}
 
inline void hashExtract(const int &x)
{
    int hash = x - (x / MOD) * MOD;
    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;
}