Cod sursa(job #1690883)

Utilizator AndreiD31Dragan Andrei AndreiD31 Data 16 aprilie 2016 08:24:06
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,i,m,k,x,xx,y,v[1000001];
int main()
{
    f>>n;
    for(i=1;i<=n;i++)
        f>>v[i];

    sort(v+1,v+n+1);

    f>>m;
    for(k=1;k<=m;k++)
    {
        f>>x>>y;
        if(x==0)
        {
            xx=upper_bound(v+1,v+n+1,y)-v-1;
          if(xx>=1&&xx<=n&&v[xx]==y)g<<xx<<'\n';
          else g<<"-1"<<'\n';
        }
        else if (x==1)
        {
            xx=lower_bound(v+1,v+n+1,y+1)-v-1;
            g<<xx<<'\n';
        }

        else if (x==2)
        {
            xx=upper_bound(v+1,v+n+1,y-1)-v-1;
            g<<xx+1<<'\n';
        }
    }
    return 0;
}