Cod sursa(job #2920375)

Utilizator OvidRata Ovidiu Ovid Data 23 august 2022 20:05:25
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include<bits/stdc++.h>
using namespace std;

string numeFisier="cautbin";
ifstream fin(numeFisier+".in"); ofstream fout(numeFisier+".out");
#define cin fin
#define cout fout

#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
//#define int ll


int n, m, a[100010];



int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n; i++){
    cin>>a[i];
}
cin>>m;
for(;m--;){
    int t, x;
    cin>>t>>x;

    if(t==0){
        int l=0, r=n+1, mid=(l+r)>>1ll;
        while(l<r){

            if(a[mid]>x){
                r=mid;
            }
            else{
                l=(mid+1);
            }

            mid=(l+r)>>1ll;
        }
        mid--;
        if(a[mid]==x){
            cout<<mid<<"\n";
        }
        else{
            cout<<"-1\n";
        }
    }
    if(t==1){
                int l=0, r=n+1, mid=(l+r)>>1ll;
        while(l<r){

            if(a[mid]>x){
                r=mid;
            }
            else{
                l=(mid+1);
            }

            mid=(l+r)>>1ll;
        }
        mid--;
        if(a[mid]<=x){
            cout<<mid<<"\n";
        }
        else{
            cout<<"-1\n";
        }
    }
    if(t==2){

        int l=0, r=n+1, mid=(l+r)>>1ll;
        while(l<r){

            if(a[mid]>=x){
                r=mid;
            }
            else{
                l=(mid+1);
            }

            mid=(l+r)>>1ll;
        }
        //mid--;
        if(a[mid]>=x){
            cout<<mid<<"\n";
        }
        else{
            cout<<"-1\n";
        }


    }

}

return 0;
}