Pagini recente » Cod sursa (job #627305) | Cod sursa (job #2395471) | Cod sursa (job #39126) | Cod sursa (job #42301) | Cod sursa (job #2253855)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100005];
int n,i,st;
void zero(int x){
int pos=0;
int step=st;
while (step!=0){
if (pos+step<=n&&v[pos+step]<=x)
pos+=step;
step/=2;}
if (v[pos]==x)
fout<<pos<<"\n";
else
fout<<-1<<"\n";
}
void doi(int x){
int pos=0;
int step=st;
while (step!=0){
if (pos+step<=n&&v[pos+step]<x)
pos+=step;
step/=2;}
if (v[pos+1]==x)
fout<<pos+1<<"\n";
else
fout<<pos<<"\n";
}
void unu(int x){
int pos=0;
int step=st;
while (step!=0){
if (pos+step<=n&&v[pos+step]<=x)
pos+=step;
step/=2;}
if (v[pos]==x)
fout<<pos<<"\n";
else
fout<<pos+1<<"\n";
}
void ctb(int a, int b){
cout<<a;
if (a==0)
zero(b);
if (a==1)
unu(b);
if (a==2)
doi(b);
}
int main()
{
fin>>n;
st=1;
while(st<n)
st*=2;
for (i=1;i<=n;i++){
fin >>v[i];
}
int m;
int x,y;
fin >>m;
for (int j=1;j<=m;j++)
{
fin>>x>>y;
ctb(x,y);
}
return 0;
}