Cod sursa(job #2883318)

Utilizator MafteiDavidAndreiMaftei David-Andrei MafteiDavidAndrei Data 1 aprilie 2022 13:34:44
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>

using namespace std;
ifstream a ("cautbin.in");
ofstream b ("cautbin.out");
int i,v[100005],n,m,c,x,st,dr,mij,poz;
int main()
{
    a>>n;
    for (i=1;i<=n;i++)
    {
        a>>v[i];
    }
    a>>m;
    for (i=1;i<=m;i++)
    {
        a>>c>>x;
        if (c==0)
        {
            st=1;
            dr=n;
            poz=-1;
            while (st<=dr)
            {
                mij =(st+dr)/2;
                if (v[mij]<x)
                {
                    st=mij+1;
                }
                if (v[mij]==x)
                {
                    poz=mij;
                    st=mij+1;
                }
                if (v[mij]>x)
                {
                    dr=mij-1;
                }
            }
            b<<poz<<'\n';
        }
        if (c==1)
        {
            st=1;
            dr=n;
            poz=0;
            while (st<=dr)
            {
                mij =(st+dr)/2;
                if (v[mij]<=x)
                {
                    st=mij+1;
                    poz=mij;
                }
                if (v[mij]>x)
                {
                    dr=mij-1;
                }
            }
            b<<poz<<'\n';
        }
        if (c==2)
        {
            st=1;
            dr=n;
            poz=0;
            while (st<=dr)
            {
                mij =(st+dr)/2;
                if (v[mij]<x)
                {
                    st=mij+1;
                }
                if (v[mij]>=x)
                {
                    dr=mij-1;
                    poz=mij;
                }
            }
            b<<poz<<'\n';
        }
    }
    return 0;
}