Cod sursa(job #340462)

Utilizator alexandru92alexandru alexandru92 Data 14 august 2009 18:43:07
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 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();  //--ibegin;
    iend=v.end(); //++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 {
                    v[x].first=v[x].second=0;
                    s=v.equal_range(x);
                    if( 1 == q )
                    {
                        --s.first;
                        out<<s.first->second.second;

                    }
                    else if( 2 == q )
                         {
                            out<<s.second->second.first;
                         }
                    v.erase(x);
                 }
        }
        else {
                if( q <= 1 ) out<<v[x].second;
                else out<<v[x].first;
             }
        out<<'\n';
    }
    return 0;
}