Cod sursa(job #2664920)
Utilizator | Data | 29 octombrie 2020 18:45:14 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.38 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,v[100001],x,y,rep;
int main()
{
fin>>n;
for (int i=1;i<=n;i++)
fin>>v[i];
fin>>rep;
for (int i=1;i<=rep;i++){
fin>>x>>y;
if (x==0){
int st=1;
int dr=n;
while (st<=dr){
long long mid=(st+dr)/2;
if (v[mid]<=y)
st=mid+1;
else
dr=mid-1;
}
if (v[dr]==y)
fout<<dr<<"\n";
else
fout<<-1<<"\n";
}
else
if (x==1){
int st=1;
int dr=n;
while (st<=dr){
long long mid=(st+dr)/2;
if (v[mid]<=y)
st=mid+1;
else
dr=mid-1;
}
fout<<dr<<"\n";
}
else{
int st=1;
int dr=n;
while (st<=dr){
long long mid=(st+dr)/2;
if (v[mid]<y)
st=mid+1;
else
dr=mid-1;
}
fout<<st<<"\n";
}
}
return 0;
}