Cod sursa(job #2378673)

Utilizator GabyD002Dobrita Gabriel GabyD002 Data 12 martie 2019 15:36:31
Problema Cautare binara Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 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
        {   bool ok=false;
            do
            {   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) ok=true;
                    else
                        if(t==1) x--;
                            else x++;
            }
            while(!ok);
            int up=upper_bound(v+1,v+n+1,x)-v;
            int lw=lower_bound(v+1,v+n+1,x)-v;
            if(t==1) g<<up-1<<'\n';
                else g<<lw<<'\n';
        }
    }
    return 0;
}