Pagini recente » Cod sursa (job #2674371) | Cod sursa (job #1326154) | Cod sursa (job #1827890) | Cod sursa (job #1426412) | Cod sursa (job #2450130)
#include <iostream>
#include <fstream>
#define sh short
#define MAXN 100001
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[MAXN];
int CB(int n,int x){
int st=1,dr=n,mij=(st+dr)/2;
while(st<dr){
mij=(st+dr)/2;
if(v[mij]>x)
dr=mij-1;
else if(v[mij]<x)
st=mij+1;
else if(v[mij]==x)
st=dr=mij;
}
return st;
}
int main(){
int n,m;
fin>>n;
for(int i=1;i<=n;i++){
fin>>v[i];
}
fin>>m;
for(int i=1;i<=m;i++){
sh q;
int x,poz;
fin>>q>>x;
poz=CB(n,x);
if(q==0){
if(v[poz]!=x)
fout<<-1<<'\n';
else{
while(v[poz+1]==x and poz+1<=n)
poz++;
fout<<poz<<'\n';
}
}
else if(q==1){
if(v[poz]==x){
while(v[poz+1]==x and poz+1<=n)
poz++;
fout<<poz<<'\n';
}
else if(v[poz]<x){
while(v[poz+1]<=x and poz+1<=n)
poz++;
fout<<poz<<'\n';
}
else if(v[poz]>x){
while(v[poz]>x and poz-1>=1)
poz--;
fout<<poz<<'\n';
}
}
else if(q==2){
if(v[poz]==x){
while(v[poz-1]==x and poz-1>=1)
poz--;
fout<<poz<<'\n';
}
else if(v[poz]<x){
while(v[poz]<x and poz+1<=n)
poz++;
fout<<poz<<'\n';
}
else if(v[poz]>x){
while(v[poz-1]>=x and poz-1>=1)
poz--;
fout<<poz<<'\n';
}
}
}
return 0;
}