Cod sursa(job #340429)

Utilizator alexandru92alexandru alexandru92 Data 14 august 2009 17:35:36
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
#include <map>
#include <utility>
using namespace std;
ifstream in;
ofstream out;
map< long, pair< long, long > >v;
map< long, pair< long, long > >::const_iterator ibegin,iend;
pair< map< long, pair< long, long > >::const_iterator, map< long, pair< long, long > >::const_iterator > s;
int main()
{long i,N,number,q,x;
    in.open("cautbin.in");
    in>>N;
    for( i=1; i<=N; ++i )
    {
        in>>number;
        if( !v[number].first || v[number].first > i ) v[number].first=i;
        if( !v[number].second || v[number].second < i ) v[number].second=i;
    }
    in>>N;
    ibegin=v.begin(); iend=v.end();
    --ibegin; ++iend;
    out.open("cautbin.out");
    while( N-- )
    {
        in>>q>>x;
        s=v.equal_range(x);
        if( s.second == s.first )
        {
            if( 0 == q ) out<<"-1";
            else if( 1 == q )
                 {--s.first;
                    if( s.first != ibegin ) out<<s.first->second.second;
                    //else out<<"Error";
                 }
                 else if( 2 == q )
                      {++s.second;
                        if( s.second != iend ) out<<s.second->second.first;
                        //else out<<"Error";
                      }
        }
        else {
                if( q <= 1 ) out<<v[x].second;
                else out<<v[x].first;
             }
        out<<'\n';
    }
    return 0;
}