Cod sursa(job #2218583)

Utilizator PetrisorAnghel Petrisor Petrisor Data 5 iulie 2018 08:53:07
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
#include <fstream>
using namespace std;

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

int a[100010];

int main(){
    int n, m, x, y, t, i;
    f >> n;
    for(i = 1; i <= n; i++)
        f >> a[i];

    f >> m;
    while(m --){
        f >> t >> y;
        if(t == 0){
                x = upper_bound(a + 1, a + n + 1, y) - a - 1;
                if(a[x] == y)
                    g << x << endl;
                else
                    g << " -1 " << endl;


        } else if(t == 1){
            x = lower_bound(a + 1, a + n + 1, y + 1) - a - 1;
            g << x << endl;
        } else {
            x = upper_bound(a + 1, a + n + 1, y - 1) - a;
            g << x << endl;
    }


}
   return 0;
}