Cod sursa(job #582250)

Utilizator BitOneSAlexandru BitOne Data 15 aprilie 2011 09:29:45
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX

using namespace std;
vector< int > v;
pair< vector<int>::iterator, vector<int>::iterator > x;
int main( void )
{
    int N, M, op, y;
    ifstream in( "cautbin.in" );
    in>>N;
    for( op=1; op <= N; ++op )
    {
        in>>y;
        v.push_back(y);
    }
    ofstream out( "cautbin.out" );
    for( in>>M; M; --M )
    {
        in>>op>>y;
        x=equal_range( v.begin(), v.end(), y );
        switch(op)
        {
            case 0 : out<<( x.first == x.second ? -1 : x.second-v.begin() )<<'\n'; break;
            case 1 : out<<( x.second-v.begin() )<<'\n'; break;
            case 2 : out<<( x.first-v.begin()+1 )<<'\n';
        }
    }
    return EXIT_SUCCESS;
}