Cod sursa(job #1414376)

Utilizator irimiecIrimie Catalin irimiec Data 2 aprilie 2015 16:05:31
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 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

#ifndef ONLINE_JUDGE
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
#endif

const int MAXN = 100010;

int n;
int v[MAXN];

void read() {
	fin >> n;
	for(int i = 0; i < n; ++i)
	    fin >> v[i];

    int T, t, val, step, lg, i;
    for(step = 1; step < n; step <<= 1);
    fin >> T;

    while(T--) {
        fin >> t >> val;
        if(t < 2) {
            lg = step;
            for(i = 0; lg; lg >>= 1) {
                if(i + lg < n && v[i + lg] <= val)
                    i += lg;
            }

            if(!t && v[i] != val)
                fout << "-1\n";
            else
                fout << (i + 1) << "\n";
        } else {
            lg = step;
            for(i = n; lg; lg >>= 1) {
                if(i - lg >= 0 && v[i - lg] >= val)
                    i -= lg;
            }

            fout << (i + 1) << "\n";
        }
    }
}

int main() {
	read();

    return 0;
}