Cod sursa(job #3360507)
| Utilizator | Data | 14 iulie 2026 14:32:55 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 40 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.1 kb |
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
long long v[100001];
int main()
{
long long n,i,st,dr,mij,q,tipq,x;
fin>>n;
for(i=1;i<=n;i++)
fin>>v[i];
fin>>q;
while(q--){
fin>>tipq>>x;
if(tipq==0){
st=1;
dr=n;
while(dr-st>1){
mij=st+(dr-st)/2;
if(v[mij]<=x)
st=mij;
else
dr=mij;
}
if(v[st]==x)
fout<<st<<'\n';
else
fout<<-1<<'\n';
}
else if(tipq==1){
st=1;
dr=n;
while(dr-st>1){
mij=st+(dr-st)/2;
if(v[mij]<=x)
st=mij;
else
dr=mij;
}
fout<<st<<'\n';
}
else if(tipq==2){
st=0;
dr=n-1;
while(dr-st>1){
mij=st+(dr-st)/2;
if(v[mij]<x)
st=mij;
else
dr=mij;
}
fout<<dr<<'\n';
}
}
}