Cod sursa(job #2053322)

Utilizator Dinu2005Dinu I Dinu2005 Data 31 octombrie 2017 17:54:01
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <iostream>
#include <fstream>
using namespace std;
int v[100005];
int main()
{
    ifstream cin("cautbin.in");
    ofstream cout("cautbin.out");
    int n,i,op,sol,tip,x,st,dr;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>v[i];
    }
    cin>>op;
    for(i=1;i<=op;i++){
        cin>>tip>>x;
        if(tip==0){
            st=1;
            dr=n;
            sol=-1;
            while(st<=dr){
                int poz=1LL*(st+dr)/2;
                if(v[poz]>x){
                    dr=poz-1;
                }
                else if(v[poz]<x){
                    st=poz+1;
                }
                else if(v[poz]==x){
                    sol=poz;
                    st=poz+1;
                }
            }
        }
        else if(tip==1){
            st=1;
            dr=n;
            sol=0;
            while(st<=dr){
                int poz=(st+dr)/2;
                if(v[poz]>x){
                    dr=poz-1;
                }
                else if(v[poz]<=x){
                    st=poz+1;
                    sol=poz;
                }
            }
        }
        else if(tip==2){
             st=1;
            dr=n;
            sol=0;
            while(st<=dr){
                int poz=(st+dr)/2;
                if(v[poz]<x){
                    st=poz+1;
                }
                else if(v[poz]>=x){
                    dr=poz-1;
                    sol=poz;
                }
            }
        }
        cout<<sol<<'\n';
    }
    return 0;
}