Cod sursa(job #3326156)

Utilizator baiesu_sebastian_mihaiBaiesu Sebastian Mihai baiesu_sebastian_mihai Data 27 noiembrie 2025 16:07:21
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>

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