Pagini recente » Cod sursa (job #1119566) | Cod sursa (job #1759284) | Cod sursa (job #2894728) | Cod sursa (job #2695208) | Cod sursa (job #803966)
Cod sursa(job #803966)
#include<fstream>
using namespace std;
int main(){
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, a[100000], r=-1, aux, st, dr, i, j, x, m;
fin >> n;
for(i=0; i<n; i++){
fin >> a[i];
}
fin >> m;
for(i=0; i<m; i++){
st = 0;
dr = n-1;
fin >> j >> x;
if(j == 0){
while(st<dr){
aux = (st + dr)/2;
if(x >= a[aux]) {
r = aux;
st = aux + 1;
}
else dr = aux;
}
if(st == dr && a[st]==x) r = st;
fout << r+1 << endl;
}
else if(j == 1){
while(st<dr){
aux = (st + dr)/2;
if(x >= a[aux]){
r = aux;
st = aux+1;
}
else dr= aux;
}
if(st == dr && a[st]==x) r = st;
fout << r+1 << endl;
}
if(j == 2){
while(st < dr){
aux = (st + dr)/2;
if(x <= a[aux]){
r = aux;
dr = aux;
}
else st = aux+1;
}
if(st == dr && x==a[aux]) r = st;
fout << r+1 << endl;
}
}
fin.close();
fout.close();
return 0;
}