Cod sursa(job #3294344)

Utilizator SfichiAndreiSfichi Andrei SfichiAndrei Data 21 aprilie 2025 19:56:17
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int N,M,v[100009],tip,x;
int main()
{
    fin>>N;
    for(int i=1;i<=N;i++)
    {
        fin>>v[i];
    }
    fin>>M;
    while(M--)
    {
        fin>>tip>>x;
        if(tip==0)
        {
            int poz=-1,l=1,r=N,mij;
            while(l<=r)
            {
                mij=(l+r)/2;
                if(v[mij]==x)
                {
                    poz=mij;
                    l=mij+1;
                }
                if(v[mij]>x)
                {
                    r=mij-1;
                }
                if(v[mij]<x)
                {
                    l=mij+1;
                }
            }
            fout<<poz<<'\n';
        }
        if(tip==1)
        {
            int poz=-1,l=1,r=N,mij;
            while(l<=r)
            {
                mij=(l+r)/2;
                if(v[mij]<=x)
                {
                    poz=mij;
                    l=mij+1;
                }
                if(v[mij]>x)
                {
                    r=mij-1;
                }
            }
            fout<<poz<<'\n';
        }
        if(tip==2)
        {
            int poz=-1,l=1,r=N,mij;
            while(l<=r)
            {
                mij=(l+r)/2;
                if(v[mij]>=x)
                {
                    poz=mij;
                    r=mij-1;
                }
                if(v[mij]<x)
                {
                    l=mij+1;
                }
            }
            fout<<poz<<'\n';
        }
    }
    return 0;
}