Cod sursa(job #2327265)

Utilizator marius0072scarlat marius stefan marius0072 Data 24 ianuarie 2019 16:07:10
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
	
#include <stdio.h>
#include <algorithm>
using namespace std;
 
int V[100100];
 
int main () {
    int N, M, x, y, i, t;
 
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
 
    scanf("%d", &N);
 
    for (i = 1; i <= N; ++ i)
        scanf("%d", &V[i]);
 
    sort(V + 1, V + N + 1);
 
    scanf("%d", &M);
    
    for (i = 1; i <= M; ++ i) {
        scanf("%d%d", &t, &y);
 
        if (t == 0) {
            x = upper_bound(V + 1, V + N + 1, y) - V - 1;
            if (x <= N && x >= 1 && V[x] == y)
                printf("%d\n", x);
            else
                printf("-1\n");
        } else if (t == 1) {
            x = lower_bound(V + 1, V + N + 1, y + 1) - V - 1;
            printf("%d\n", x);
        } else {
            x = upper_bound(V + 1, V + N + 1, y - 1) - V;
            printf("%d\n", x);
        }
    }
}