Pagini recente » Cod sursa (job #549810) | Cod sursa (job #2576767) | Cod sursa (job #790619) | Cod sursa (job #2550051) | Cod sursa (job #3165217)
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
int n,m,p,i,j,v[100000],mid,st,dr,x,poz;
int main(){
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin>>n;
for(i=0;i<n;i++){
fin>>v[i];
}
fin>>m;
for(i=0;i<m;i++){
fin>>p>>x;
if(p==0){
st=0; dr=n-1; poz=-2;
while(st<=dr){
mid=(st+dr)/2;
if(x==v[mid]){
poz=mid;
st=mid+1;
}else{
if(x<v[mid]){
dr=mid-1;
}else{
st=mid+1;
}
}
}
fout<<poz+1<<'\n';
}else if(p==1){
st=0; dr=n-1; poz=-1;
while(st<=dr){
mid=(st+dr)/2;
if(x<v[mid]){
dr=mid-1;
}else{
st=mid+1;
poz=mid;
}
}
fout<<poz+1<<'\n';
}else{
st=0; dr=n-1; poz=-1;
while(st<=dr){
mid=(st+dr)/2;
if(x<=v[mid]){
dr=mid-1;
poz=mid;
}else{
st=mid+1;
}
}
fout<<poz+1<<'\n';
}
}
return 0;
}