Pagini recente » Cod sursa (job #2324315) | Cod sursa (job #1421573) | Cod sursa (job #2404328) | Cod sursa (job #2702014) | Cod sursa (job #2763049)
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n, m, v[100001], tip, x;
int cb(int a)
{
int pw=1, pos=0;
while(pw<=n)
pw*=2;
pw/=2;
while(pw)
{
if(pos+pw<=n)
if(v[pos+pw]<=a)
pos+=pw;
pw/=2;
}
return pos;
}
int cb2(int a)
{
int pw=1, pos=0;
while(pw<=n)
pw*=2;
pw/=2;
while(pw)
{
if(pos+pw<=n)
if(v[pos+pw]<a)
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++)
{
fin>>tip>>x;
if(tip==0)
{
int poz=cb(x);
if(v[poz]==x)
fout<<poz<<'\n';
else fout<<-1<<'\n';
}
else if(tip==1)
{
fout<<cb(x)<<'\n';
}
else
{
fout<<cb2(x)<<'\n';
}
}
return 0;
}