Cod sursa(job #2066109)

Utilizator mateidanvladMatei Vlad mateidanvlad Data 14 noiembrie 2017 18:20:52
Problema Cautare binara Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
using namespace std;

int pas,l=16,r=0,n,x,v[100000],m,a;

ifstream f("cautbin.in");
ofstream g("cautbin.out");

void caut0 (int x) {
    r=0;
    pas = 1<<l;
    while(pas!=0) {
        if(r+pas<=n && v[r+pas]<=x) {
            r+=pas;
        }
        pas/=2;
    }
    if(r>0 && v[r]==x) {
        g<<r<<"\n";
    } else {
        g<<-1<<"\n";
    }
}

void caut1 (int x) {
    r=0;
    pas = 1<<l;
    while(pas!=0) {
        if(r+pas<=n && v[r+pas]<=x) {
            r+=pas;
        }
        pas/=2;
    }
    g<<r<<"\n";
}

void caut2 (int x) {
    r=0;
    pas = 1<<l;
    while(pas!=0) {
        if(r+pas<=n && v[r+pas]<x) {
            r+=pas;
        }
        pas/=2;
    }
    r++;
    g<<r<<"\n";
}

int main() {
    f>>n;
    for(int i=1;i<=n;i++) {
        f>>v[i];
    }
    f>>m;
    for(int i=1;i<=m;i++) {
        f>>a>>x;
        if(a==0) caut0(x);
        else {
            if(a==1) caut1(x);
            else caut2(x);
        }
    }
    return 0;
}