Cod sursa(job #2228986)

Utilizator ciutanpCiuta Andrei Calin ciutanp Data 5 august 2018 16:42:22
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");

int n,v[100005],x,op;
int main()
{
   f>>n;
   for(int i=1;i<=n;++i)
    f>>v[i];
   int k;
   f>>k;
   for(int i=1;i<=k;++i)
   {
       int ras;
       f>>op>>x;
       if(op==0)
       {
           ras=upper_bound(v+1,v+n+1,x)-v-1;
           if(ras>=1 && ras<=n && v[ras]==x)
           g<<ras<<'\n';
           else
            g<<-1<<'\n';

       }
       else
        if(op==1)
       {
           ras=lower_bound(v+1,v+n+1,x+1)-v-1;
           g<<ras<<'\n';
       }
       else
       {
           ras=upper_bound(v+1,v+n+1,x-1)-v;
           g<<ras<<'\n';
       }
   }

}

/*bool ok=1;
            set<int>::iterator it1,it2;
            for(set<int>::iterator it=My_set.begin();it != My_set.end() && ok;++it)
            if(*it>=x )
            {
                it1=it;
                ok=0;
            }
             ok=1;
            for(set<int>::iterator it=My_set.begin();it != My_set.end() && ok;++it)
            if(*it>y )
            {
                it2=it--;
                ok=0;
            }

            My_set.erase(it1,it2);
*/