Cod sursa(job #1114184)

Utilizator bogdanpaunFMI Paun Bogdan Gabriel bogdanpaun Data 21 februarie 2014 13:03:54
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
#include <stdio.h>
#include <map>
using namespace std;

multimap<int,int > M;
multimap<int,int >::iterator it,it2;
int N;


int main()
{
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    scanf("%d",&N);
    int X;
    for(register int i=1;i<=N;++i){
        scanf("%d",&X);
        M.insert( make_pair(X,i) );
    }
    int k,a,b;
    scanf("%d",&k);
    for(register int i=1;i<=k;++i){
        scanf("%d%d",&a,&b);
        switch( a ){
            case 0:{
                it= M .find( b );
                it2=it;
                if( it != M.end() ){
                    while( (++it)->first == it2->first ) ;
                    --it;

                }
                it2=M.begin();
                it2--;
                 if( it!=M.end() && it !=it2 && it->first>0)
                printf("%d\n", it->second );
                else  printf("-1\n");
                break;
            }
            case 1:{
                it=M.find(b);
                it2=it;
                if( it != M.end() ){
                    while( (++it)->first == it2->first ) ;
                    --it;
                }
                else{
                    it=M.upper_bound(b);
                    --it;
                }
                it2=M.begin();
                it2--;
               if( it!=M.end() && it !=it2 && it->first>0)
                printf("%d\n", it->second );
                else  printf("-1\n");
                break;
            }
            case 2:{
                it=M.upper_bound(b);
                if( it->second > M.find(b)->second  ){
                    it= M .find( b );
                }
                it2=M.begin();
                it2--;
                 if( it!=M.end() && it !=it2 && it->first>0)
                printf("%d\n", it->second );
                else  printf("-1\n");
                break;
            }
        }
    }


    return 0;
}