Cod sursa(job #1364556)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 27 februarie 2015 18:36:33
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdio>
#include <vector>
#include <algorithm>



#define DIM 66013

using namespace std;

char buffer[DIM];
int poz = DIM - 1;

void Scanf(int &A){
    A = 0;
    while('0' > buffer[poz] || buffer[poz] > '9')
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    while('0' <= buffer[poz] && buffer[poz] <= '9')
    {
        A = A * 10 + buffer[poz] - 48;
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    }
}

vector<int> v;
int N,M;

void Read()
{
    Scanf(N);
    v.resize(N);
    for(int i = 0; i < N; ++i)
        Scanf(v[i]);
}

void Solve()
{
    Scanf(M);
    int tip,val,pz;
    for(int i = 1; i <= M; ++i)
    {
        Scanf(tip);
        Scanf(val);
        if(tip == 0)
        {
            pz = upper_bound(v.begin(),v.end(),val) - v.begin();
            if(v[pz-1] != val) printf("-1\n");
            else printf("%d\n",pz);
        }
        else if(tip == 1)
        {
            pz = lower_bound(v.begin(),v.end(),val+1) - v.begin();
            printf("%d\n",pz);
        }
        else if(tip == 2)
        {
            pz = upper_bound(v.begin(),v.end(),val-1) - v.begin() + 1;
            printf("%d\n",pz);
        }
    }
}

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

    return 0;
}