Cod sursa(job #1776105)

Utilizator MithrilBratu Andrei Mithril Data 10 octombrie 2016 22:35:19
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;
queue<int>Q;
int main()
{
    long long n,m,x,y;
    vector<long long>::iterator it;
    cin>>n;
    vector<long long> V(n+1);
    for(int i=1; i<=n; i+=1)
    {
        cin>>x;
        V[i]=x;
    }
    cin>>m;
    for(int i=0; i<m; i+=1)
    {
        cin>>x>>y;
        if(x==0)
        {
            it = upper_bound(V.begin()+1,V.end(),y);
            if(it!=V.end())
                Q.push(it-V.begin()-1);
            else
                Q.push(-1);
        }
        if(x==1)
        {
            it = lower_bound(V.begin()+1,V.end(),y+1);
            Q.push(it-V.begin()-1);
        }
        if(x==2)
        {
            it = upper_bound(V.begin()+1,V.end(),y-1);
            Q.push(it-V.begin());
        }
    }
    while(!Q.empty()){
        cout<<Q.front()<<'\n';
        Q.pop();
    }
    return 0;
}