Cod sursa(job #2172439)
Utilizator | Data | 15 martie 2018 16:30:43 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.66 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
int n,m,v[100011],i,x,y,t;
int main()
{
in>>n;
for(i=1;i<=n;i++) in>>v[i];
in>>m;
for(i=1;i<=m;i++)
{
in>>t>>x;
if(t==0)
{
y=upper_bound(v+1,v+n+1,x)-v-1;
if(v[y]==x) out<<y<<'\n';
else out<<-1<<'\n';
}
else if(t==1)
{
y=lower_bound(v+1,v+n+1,x+1)-v-1;
out<<y<<'\n';
}
else
{
y=upper_bound(v+1,v+n+1,x-1)-v;
out<<y<<'\n';
}
}
return 0;
}