Cod sursa(job #1776245)

Utilizator MithrilBratu Andrei Mithril Data 11 octombrie 2016 08:17:54
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;
int main()
{
    ifstream fin("cautbin.in");
    ofstream fout("cautbin.out");
    int n,m,x,y;
    vector<int>::iterator it;
    fin>>n;
    vector<int> V(n+1);
    for(int i=1; i<=n; i+=1)
    {
        fin>>x;
        V[i]=x;
    }
    fin>>m;
    for(int i=0; i<m; i+=1)
    {
        fin>>x>>y;
        switch(x){
        case 0:
            it = upper_bound(V.begin()+1,V.end(),y);
            x=it-V.begin()-1;
            if(x>=1&&x<=n&&V[x]==y)
                fout<<x<<'\n';
            else
                fout<<-1<<'\n';
                break;
        case 1:
            it = lower_bound(V.begin()+1,V.end(),y+1);
            x=it-V.begin()-1;
            fout<<x<<'\n';
            break;
        case 2:
            it = upper_bound(V.begin()+1,V.end(),y-1);
            x=it-V.begin();
            fout<<x<<'\n';
            break;
    }
    fin.close();
    fout.close();
    return 0;
}