Cod sursa(job #1363339)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 26 februarie 2015 21:37:54
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

int n, Q;
int v[100010];

FILE *in = fopen("cautbin.in", "r");
FILE *out = fopen("cautbin.out", "w");

int main()
{
    int x;

    fscanf(in, "%d", &n);
    for(int i = 1; i <= n; ++i)
        fscanf(in, "%d", &v[i]);

    int type;
    int pos;

    fscanf(in, "%d", &Q);
    while(Q--) {
        fscanf(in, "%d%d", &type, &x);

        switch(type) {
            case 0:
                pos = upper_bound(v + 1, v + n + 1, x) - v - 1;
                if(pos <= n && v[pos] == x)
                    fprintf(out, "%d\n", pos);
                else
                    fprintf(out, "%d\n", -1);
            break;

            case 1:
                pos = upper_bound(v + 1, v + n + 1, x) - v - 1;
                fprintf(out, "%d\n", pos);

            break;

            case 2:
                pos = lower_bound(v + 1, v + n + 1, x) - v;
                fprintf(out, "%d\n", pos);

            break;
        }
    }

    return 0;
}