Cod sursa(job #3349986)

Utilizator VLADIMIR_NNiculici Vladimir Andrei VLADIMIR_N Data 4 aprilie 2026 12:03:45
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 kb
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100001];
int main()
{
    int n,m,i;
    fin >> n;
    for (i=1;i<=n;i++)
    {
        fin >> v[i];
    }
    fin >> m;
    for (i=1;i<=m;i++)
    {
        int tip,x;
        fin >> tip >> x;
        if (tip==0)
        {
            int pozmax=-1,st=1,dr=n;
            while (st<=dr)
            {
                int mijloc=(st+dr)/2;
                if (v[mijloc]>x)
                {
                    dr=mijloc-1;
                }
                else if (v[mijloc]==x)
                {
                    st=mijloc+1;
                    pozmax=mijloc;
                }
                else if (v[mijloc]<x)
                {
                    st=mijloc+1;
                }
            }
            fout << pozmax << '\n';
        }
        if (tip==1)
        {
            int pozmax=-1,st=1,dr=n;
            while (st<=dr)
            {
                int mijloc=(st+dr)/2;
                if (v[mijloc]>x)
                {
                    dr=mijloc-1;
                }
                else if (v[mijloc]<=x)
                {
                    st=mijloc+1;
                    pozmax=mijloc;
                }
            }
            fout << pozmax << '\n';
        }
        if (tip==2)
        {
            int pozmax=-1,st=1,dr=n;
            while (st<=dr)
            {
                int mijloc=(st+dr)/2;
                if (v[mijloc]>=x)
                {
                    dr=mijloc-1;
                    pozmax=mijloc;
                }
                else if (v[mijloc]<x)
                {
                    st=mijloc+1;
                }
            }
            fout << pozmax << '\n';
        }
    }
    return 0;
}