Cod sursa(job #2272227)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 29 octombrie 2018 21:06:41
Problema Cautare binara Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;
int N, i, Q, Cod;
long long A[100005], Val;
int main()
{
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
    scanf("%d", &N);
    for(i = 1; i <= N; i++) scanf("%lld", &A[i]);
    scanf("%d", &Q);
    while(Q--) {
        scanf("%d%d", &Cod, &Val);
        if(Cod == 0) {
            int Poz = upper_bound(A, A + N, Val) - A - 1;
            if(A[Poz] == Val) printf("%d\n", Poz);
            else printf("-1\n");
        }
        else if(Cod == 1) {
            int Poz = upper_bound(A, A + N, Val) - A - 1;
            if(A[Poz] <= Val) printf("%d\n", Poz);
            else printf("-1\n");
        }
        else {
            int Poz = lower_bound(A, A + N, Val) - A;
            if(A[Poz] >= Val) printf("%d\n", Poz);
            else printf("-1\n");
        }
    }
    return 0;
}