Cod sursa(job #1477569)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 august 2015 15:58:20
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.53 kb
#include <bits/stdc++.h>

using namespace std;

class Scanner
{
private:

    FILE *inputFile;
    int cursor;

    static const int MAX_SIZE = 1 << 16;
    char buffer[MAX_SIZE];

    inline char getChar()
    {
        if (cursor == MAX_SIZE)
        {
            cursor = 0;
            fread(buffer, MAX_SIZE, 1, inputFile);
        }

        return buffer[cursor++];
    }

    inline int getNr()
    {
        int a = 0;
        int sign = 1;
        char ch;

        do
        {
            ch = getChar();

        } while (!isdigit(ch) && ch != '-');

        if (ch == '-')
        {
            sign = -1;
            ch = getChar();
        }

        do
        {
            a = (a << 3) + (a << 1) + (ch - '0');
            ch = getChar();

        } while (isdigit(ch));

        return a * sign;
    }

public:

    Scanner() {
    }

    Scanner(const char *file)
    {
        inputFile = fopen(file, "r");
        cursor = MAX_SIZE;
    }

    inline Scanner& operator >> (int &x)
    {
        x = getNr();
        return *this;
    }
};

class Printer
{
private:

    FILE *outputFile;
    int cursor;

    static const int MAX_SIZE = 1 << 16;
    static const int MAX_DIGIT = 20;
    char buffer[MAX_SIZE];
    char digits[MAX_DIGIT];

    inline void putChar(const char c)
    {
        buffer[cursor++] = c;

        if (cursor == MAX_SIZE)
        {
            cursor = 0;
            fwrite(buffer, 1, MAX_SIZE, outputFile);
        }
    }

    inline void writeNr(int x)
    {
        if (x < 0)
        {
            putChar('-');
            x = -x;
        }

        int p = 0, q;

        do
        {
            q = x / 10;
            digits[p++] = x - q * 10 + '0';
            x = q;

        } while (x);

        while (p--)
        {
            putChar(digits[p]);
        }
    }

public:

    Printer() {
    }

    Printer(const char *file)
    {
        outputFile = fopen(file, "w");
        cursor = 0;
    }

    inline Printer& operator << (const int &x)
    {
        writeNr(x);
        return *this;
    }

    inline Printer& operator << (const char &c)
    {
        putChar(c);
        return *this;
    }

    void fflush()
    {
        fwrite(buffer, 1, cursor, outputFile);
    }
};

template <const size_t MAX_SIZE>
class LinearProbing
{
private:

    static const int NIL = -1;
    static const int DELETED = -2;

    int table[MAX_SIZE];

    inline bool check(const size_t pos, const int key) const
    {
        return table[pos] != NIL && table[pos] != key;
    }

    int supposedPosition(const int key) const
    {
        size_t pos = 0;

        while (check((key + pos) % MAX_SIZE, key) == true)
            pos++;

        return (key + pos) % MAX_SIZE;
    }

public:

    LinearProbing()
    {
        for (int i = 0; i < MAX_SIZE; ++i)
            table[i] = NIL;
    }

    void insert(const int key)
    {
        table[supposedPosition(key)] = key;
    }

    bool find(const int key)
    {
        return table[supposedPosition(key)] == key;
    }

    void erase(const int key)
    {
        int pos = supposedPosition(key);

        if (table[pos] == key)
            table[pos] = DELETED;
    }
};

const int M = 1 << 20;

LinearProbing<M> L;

int main()
{
    Scanner in("hashuri.in");
    Printer out("hashuri.out");

    int N;
    in >> N;

    while ( N-- )
    {
        int tip, x;

        in >> tip >> x;

        switch(tip)
        {
            case 1: L.insert(x); break;
            case 2: L.erase(x); break;
            case 3: out << L.find(x) << '\n'; break;
        }
    }

    out.fflush();

    return 0;
}