Pagini recente » Cod sursa (job #917983) | Cod sursa (job #202769) | Cod sursa (job #202257) | Cod sursa (job #3314586) | Cod sursa (job #3334650)
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,v[100005];
int main(){
fin>>n;
for(int i=1;i<=n;i++) fin>>v[i];
fin>>m;
while(m--){
int t,x;
fin>>t>>x;
int st=1,dr=n,ans=-1;
if(t==0){
while(st<=dr){
int mj=st+(dr-st)/2;
if(v[mj]==x){ans=mj;st=mj+1;}
else if(v[mj]<x)st=mj+1;
else dr=mj-1;
}
}else if(t==1){
while(st<=dr){
int mj=st+(dr-st)/2;
if(v[mj]<=x){ans=mj;st=mj+1;}
else dr=mj-1;
}
}else if(t==2){
while(st<=dr){
int mj=st+(dr-st)/2;
if(v[mj]>=x){ans=mj;dr=mj-1;}
else st=mj+1;
}
}
fout<<ans<<"\n";
}
return 0;
}