Cod sursa(job #1130557)

Utilizator matei_cChristescu Matei matei_c Data 28 februarie 2014 13:58:37
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<set>
#include<map>
using namespace std ;

#define maxn 100005

int N ;
int v[maxn] ;

int Q ;

int main()
{
	std::ios_base::sync_with_stdio(false) ;

	freopen("cautbin.in", "r", stdin);
	freopen("cautbin.out", "w", stdout);

    cin >> N ;

    for(int i = 1; i <= N; ++i )
        cin >> v[i] ;

    sort( v + 1, v + N + 1 ) ;

    cin >> Q ;

    while( Q-- )
    {
        int op, x ;
        cin >> op >> x ;

        if( op == 0 )
        {
            int sol = upper_bound( v + 1, v + N + 1, x ) - v - 1 ;

            if( sol <= N && sol >= 1 && v[sol] == x )
                cout << sol << "\n" ;
            else
                cout << "-1\n" ;
        }

        if( op == 1 )
        {
            int sol = lower_bound( v + 1, v + N + 1, x + 1 ) - v - 1 ;
            cout << sol << "\n" ;
        }

        if( op == 2 )
        {
            int sol = upper_bound( v + 1, v + N + 1, x - 1 ) - v ;
            cout << sol << "\n" ;
        }
    }

	return 0 ;
}