Cod sursa(job #1776147)

Utilizator MithrilBratu Andrei Mithril Data 10 octombrie 2016 23:06:48
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 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;
        if(x==0)
        {
            it = upper_bound(V.begin()+1,V.end(),y);
            if(it!=V.end())
                fout<<(it-V.begin()-1)<<'\n';
            else
                fout<<-1<<'\n';
        }
        if(x==1)
        {
            it = lower_bound(V.begin()+1,V.end(),y+1);
            fout<<(it-V.begin()-1)<<'\n';
        }
        if(x==2)
        {
            it = upper_bound(V.begin()+1,V.end(),y-1);
            fout<<it-V.begin()<<'\n';
        }
    }
    return 0;
}