Cod sursa(job #556388)

Utilizator SpiderManSimoiu Robert SpiderMan Data 16 martie 2011 09:30:25
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
# include <algorithm>
# include <cstdio>

# define lb std :: lower_bound
# define ub std :: upper_bound

const char *FIN = "cautbin.in", *FOU = "cautbin.out" ;
const int MAX = 100005 ;

int V[MAX] ;
int N, tip, X, T ;

int main ( void ) {
    freopen ( FIN, "r", stdin ) ;
    freopen ( FOU, "w", stdout ) ;

    scanf ( "%d", &N ) ;
    for ( int i = 0; i < N; ++i ) {
        scanf ( "%d", V + i ) ;
    }
    for ( scanf ( "%d", &T ) ; T ; --T ) {
        scanf ( "%d %d", &tip, &X ) ;
        if ( tip == 0 ) {
            int aux = ub ( V , V + N , X ) - V - 1 ;
            printf ( "%d\n", aux >= 0 && aux < N && V[aux] == X ? aux + 1 : -1 ) ;
        } else if ( tip == 1 ) {
            printf ( "%d\n", lb ( V , V + N , X + 1 ) - V ) ;
        } else {
            printf ( "%d\n", ub ( V , V + N , X - 1 ) - V + 1 ) ;
        }
    }
}