Cod sursa(job #2049908)

Utilizator IVVladIon Vlad Vasile IVVlad Data 27 octombrie 2017 19:38:24
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[10000000],n,kx,intr,lo,hi,k1,y,k,x,mid;
int main()
{
    f>>n;

    for(int i=1; i<=n; i++) f>>v[i];
    f>>k;
    for(int i=1; i<=k; i++)
    {
        f>>intr>>x;
        if(intr==0)
        {
            y=upper_bound(v+1,v+n+1,x)-v;
            if(v[y-1]==x)
            {
                g<<y-1<<'\n';
            }
            else g<<-1<<'\n';
        }
        if(intr==1)
        {
            y=lower_bound(v+1,v+n+1,x)-v;
            if(v[y]==x)
            {
                y=upper_bound(v+1,v+n+1,x)-v;
                if(v[y-1]==x)
                {
                    g<<y-1<<'\n';
                }
                else g<<-1<<'\n';
            }
            else
                g<<y-1<<'\n';

        }
        if(intr==1)
        {

            y=upper_bound(v+1,v+n+1,x-1)-v;
            g<<y<<'\n';
        }
    }
    return 0;
}