Cod sursa(job #1397907)

Utilizator tudoras8tudoras8 tudoras8 Data 23 martie 2015 20:28:19
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <cstdio>
#include <algorithm>

#define MAXN 100010

using namespace std;

int main() {
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);

    int N, M, t, x, V[MAXN], y;

    scanf("%d", &N);
    for (int i = 1; i <= N; ++i) {
        scanf("%d", &V[i]);
    }

    scanf("%d", &M);

    while (M--) {
        scanf("%d %d", &t, &x);

        if (t == 0) {
            y = upper_bound(V + 1, V + N + 1, x) - (V + 1);
            if (y >= 1 && y <= N && V[y] == x)
                printf("%d\n", y);
            else
                printf("-1\n");
        } else if (t == 1) {
            y = upper_bound(V + 1, V + N + 1, x) - (V + 1);
            printf("%d\n", y);
        } else if (t == 2) {
            y = lower_bound(V + 1, V + N + 1, x) - V;
            printf("%d\n", y);
        }
    }


    return 0;
}