Cod sursa(job #1426120)
| Utilizator | Data | 28 aprilie 2015 23:12:53 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <fstream>
using namespace std;
ifstream f("cautbin.in"); ofstream g("cautbin.out");
int a[100001];
int main()
{ int n,m,i,t,x,j,step;
in>>n;
for(i=1;i<=n;i++) f>>v[i];
f>>m;
for(j=1;j<=m;j++)
{
f>>t>>x;
pas=1<<16;
i=0;
if(t==0 || t==1)
{
while(step!=0)
{
if(i+step<=n && a[i+pas]<=x) i+=step;
pas/=2;
}
if(t==0&&v[i]!=x) g<<-1<<"\n";
else g<<i<<"\n";
}
else
{
while(step!=0)
{
if(i+step<=n && a[i+step]<x) i+=step;
step/=2;
}
g<<i+1<<"\n";
}
}
return 0;
}