Cod sursa(job #3228607)
Utilizator | Popescu George popescu_george | Data | 9 mai 2024 08:17:49 |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.55 kb |
#include<fstream>
using namespace std;
ifstream F("cautbin.in");
ofstream G("cautbin.out");
int a[100000],n,i,j,k,l,t,m;
int main()
{
for(F>>n;i<n;F>>a[i++]);
for(F>>m,k=1;k<n;k<<=1);
for(;m--;)
if(F>>l>>j,l<2) {
for(i=0,t=k;t;t>>=1)
if(i+t<n&&a[i+t]<=j)
i+=t;
G<<(!l&&a[i]!=j?-1:i+1)<<'\n';
} else {
for(i=n-1,t=k;t;t>>=1)
if(i>=t&&a[i-t]>=j)
i-=t;
G<<i+1<<'\n';
}
return 0;
}