Cod sursa(job #2911553)

Utilizator Deleanu_LucaDeleanu Luca Deleanu_Luca Data 30 iunie 2022 14:12:34
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cautbin.in");
ofstream fout("cautbin.out");

unsigned int n,v[100001],m,i,x,st,dr,mij,c;
int poz;

int main()
{
    fin>>n;
    for(i=1; i<=n; i++)
        fin>>v[i];
    fin>>m;
    for(i=1; i<=m; i++)
    {
        fin>>c>>x;
        st=1; dr=n;
        if(c==0)
        {
            poz=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]==x)
                {
                    poz=mij;
                    st=mij+1;
                }
                else if(v[mij]<x)
                {
                    st=mij+1;
                }
                if(v[mij]>x)
                {
                    dr=mij-1;
                }

            }
            fout<<poz<<'\n';
        }
        else if(c==1)
        {
            poz=1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]<=x)
                {
                    poz=mij;
                    st=mij+1;
                }
                else if(v[mij]>x)
                {
                    dr=mij-1;
                }

            }
            fout<<poz<<'\n';
        }
        else
        {
            poz=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    poz=mij;
                    dr=mij-1;
                }
                else if(v[mij]<x)
                {
                    st=mij+1;
                }

            }
            fout<<poz<<'\n';
        }

    }
    return 0;
}