Cod sursa(job #2378697)

Utilizator GabyD002Dobrita Gabriel GabyD002 Data 12 martie 2019 15:51:03
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define NM 100005
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[NM],vmin[NM],vmax[NM];
int main()
{   int n;
    f>>n;
    for(int i=1; i<=n; i++) f>>v[i];
    int k;
    f>>k;
    while(k--)
    {   int t,x,poz=-1;
        f>>t>>x;
        if(!t)
        {   int lw=lower_bound(v+1,v+n+1,x)-v;
            int up=upper_bound(v+1,v+n+1,x)-v;
            if(up-lw) poz=up-1;
            g<<poz<<'\n';
        }
        else
        {   int lw=lower_bound(v+1,v+n+1,x)-v;
            int up=upper_bound(v+1,v+n+1,x)-v;
            if(t==1) g<<up-1<<'\n';
                else g<<lw<<'\n';
        }
    }
    return 0;
}