Pagini recente » Cod sursa (job #1296284) | Cod sursa (job #1674129) | Cod sursa (job #2336769) | Cod sursa (job #235619) | Cod sursa (job #2861750)
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n, m, v[100001];
int cautare_binara1(int x)
{
int pw=1, pos=0;
while(pw<=n)
pw*=2;
pw/=2;
while(pw>=1)
{
if(pos+pw<=n)
if(v[pos+pw]<=x)
pos+=pw;
pw/=2;
}
if(v[pos]==x) return pos;
else return -1;
}
int cautare_binara2(int x)
{
int pw=1, pos=0;
while(pw<=n)
pw*=2;
pw/=2;
while(pw>=1)
{
if(pos+pw<=n)
if(v[pos+pw]<=x)
pos+=pw;
pw/=2;
}
return pos;
}
int cautare_binara3(int x)
{
int pw=1, pos=0;
while(pw<=n)
pw*=2;
pw/=2;
while(pw>=1)
{
if(pos+pw<=n)
if(v[pos+pw]<x)
pos+=pw;
pw/=2;
}
return pos+1;
}
int main()
{
fin>>n;
for(int i=1; i<=n; i++)
fin>>v[i];
fin>>m;
for(int i=1; i<=m; i++)
{
int x, y;
fin>>x>>y;
if(x==0)
fout<<cautare_binara1(y);
else if(x==1)
fout<<cautare_binara2(y);
else fout<<cautare_binara3(y);
fout<<'\n';
}
return 0;
}