Cod sursa(job #1706167)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 21 mai 2016 19:17:11
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.74 kb
# include <iostream>

# include <vector>

# include <stdio.h>
# include <stdlib.h>

# include <cstdio>

bool isDigit[128];

using namespace std;

const int HASH_SIZE = 16383; /// ( 1 << 14 ) - 1

class myHash {
    vector<int> v[HASH_SIZE + 1];

    public:
        inline bool check( int val ) {
            unsigned int i, h;

            h = val & HASH_SIZE;

            i = 0;
            while ( i < v[h].size() && v[h][i] != val )
                i ++;

            return ( i < v[h].size() );
        }

        inline void pop( int val ) {
            unsigned int i, h;

            h = val & HASH_SIZE;

            i = 0;
            while ( i < v[h].size() && v[h][i] != val  )
                i ++;

            if ( i < v[h].size() ) {
                v[h][i] = v[h][v[h].size() - 1];
                v[h].pop_back();
            }
        }

        inline void push( int val ) {
            unsigned int h = val & HASH_SIZE;

            v[h].push_back( val );
        }
};

class ifbuffer {
    private:
        char text[65536];
        int size;
        int pos;

        FILE *file;

        inline void refill( void ) {
            pos = 0;
            fread( text, 1, size, file );
        }

        inline char getc() {
            char c = text[pos ++];

            if ( pos == size )
                refill();

            return c;
        }

        inline int getnr() {
            char c = getc();
            int nr = 0;

            while ( !isDigit[c] )
                c = getc();
            while ( isDigit[c] ) {
                nr = nr * 10 + c - '0';
                c = getc();
            }

            return nr;
        }

    public:
        inline ifbuffer( char * path, int s = 100000 ) {
            size = s;

            file = fopen( path, "r" );

            refill();
        }

        inline ifbuffer &operator>>( int &v ) {
            v = getnr();
            return *this;
        }

        inline ifbuffer &operator>>( char &v ) {
            v = getc();
            return *this;
        }

        inline void close() {
            fclose( file );
        }
};

class myHash m;

int main() {
    ifbuffer fin( "hashuri.in" );
    FILE * fout = fopen( "hashuri.out", "w" );

    char ofbuffer[2000000];

    int n, i, t, nr, k;

    for ( i = '0'; i <= '9'; i ++ )
        isDigit[i] = true;

    fin >> n;

    k = 0;
    for ( i = 0; i < n; i ++ ) {
        fin >> t >> nr;

        switch( t ) {
        case 1:
            m.push( nr );
            break;
        case 2:
            m.pop( nr );
            break;
        case 3:
            ofbuffer[k ++] = '0'+ m.check( nr );
            ofbuffer[k ++] = '\n';
            break;
        }
    }

    fwrite( ofbuffer, 1, k, fout );

    fin.close();
    fclose( fout );

    return 0;
}