Cod sursa(job #1809866)

Utilizator turnenTudor Oprescu turnen Data 19 noiembrie 2016 13:09:05
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>

using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
const int L=16;
int v[100001];
int caut(int l,int a,int x)
{
    int r=0,pas=1<<L;
        if(a<=1)
        {
            while(pas!=0)
    {
            if(r+pas<=l&&v[r+pas]<=x)
            {
                r+=pas;
            }
             pas/=2;
    }
        }
        else
        {
            while(pas!=0)
    {
            if(r+pas<=l&&v[r+pas]<x)
            {
                r+=pas;
            }
             pas/=2;
    }
        }

    if(a==0)
    {
        if(v[r]==x){
        return r;
        }else{
        return-1;
        }
    }
    else if(a==1)
    {
        return r;
    }
    else if(a==2)
    {
        return 1+r;

    }

}
int main()
{
    int n,t,a,b,i;
    in>>n;
    for(i=1; i<=n; i++)
    {
        in>>v[i];
    }
    in>>t;
    for(i=1; i<=t; i++)
    {
        in>>a>>b;
        out<<caut(n,a,b)<<'\n';
    }
}