Cod sursa(job #1706441)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 22 mai 2016 16:44:21
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.79 kb
# include <fstream>

# define MAX_N 1000001

int v[MAX_N], n;

//# define v ( v - 1 )

using namespace std;

class ifbuffer {
    private:
        char text[100000];
        int size;
        int pos;
        bool isDigit[128];

        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" );

            int i;
            for ( i = 0; i < '0'; i ++ )
                isDigit[i] = false;
            for ( i = '0'; i <= '9'; i ++ )
                isDigit[i] = true;
            for ( i = '9' + 1; i < 128; i ++ )
                isDigit[i] = false;

            refill();
        }

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

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

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

int cbin_equal( int val ) {
    int pos, pas;

    pos = 0;
    for ( pas = 20; pas >= 0; pas -- ) {
        if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] <= val )
            pos += ( 1 << pas );

    }

    return ( pos != 0 && v[pos] == val ? pos : -1 );
}

int cbin_smaller( int val ) {
    int pos, pas;

    pos = 0;
    for ( pas = 20; pas >= 0; pas -- )
        if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] <= val )
            pos += ( 1 << pas );

    return pos;
}

int cbin_bigger( int val ) {
    int pos, pas;

    pos = 0;
    for ( pas = 20; pas >= 0; pas -- )
        if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] < val )
            pos += ( 1 << pas );

    return pos + 1;
}

int main() {
    ifbuffer fin( "cautbin.in" );
    ofstream fout( "cautbin.out" );

    int i, m, x, t, p;

    fin >> n;

    for ( i = 1; i <= n; i ++ )
        fin >> v[i];

    fin >> m;

    for ( i = 1; i <= m; i ++ ) {
        fin >> t >> x;

        switch( t ) {
        case 0:
            fout << cbin_equal( x );
            break;
        case 1:
            fout << cbin_smaller( x );
            break;
        case 2:
            fout << cbin_bigger( x );
            break;
        }

        if ( i != m )
            fout << endl;
    }

    fin.close();
    fout.close();

    return 0;
}