Pagini recente » Cod sursa (job #1991927) | Cod sursa (job #1124779) | Cod sursa (job #1809833) | Cod sursa (job #1777299) | Cod sursa (job #3327967)
#include <bits/stdc++.h>
#define nmax 200000
using namespace std;
#define cin fin
#define cout fout
ifstream cin("cautbin.in");
ofstream cout("cautbin.out");
int n, op, x, y, q;
int v[200000];
int cb1(int x)
{
int st=1, dr=n, poz=-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(v[mid]<x)
{
st=mid+1;
}
else if(v[mid]>x)
{
dr=mid-1;
}
else
{
poz=mid;
st=mid+1;
}
}
return poz;
}
int cb2(int x)
{
int st=1, dr=n, poz=-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(v[mid]<=x)
{
poz=mid;
st=mid+1;
}
else if(v[mid]>x)
dr=mid-1;
}
return poz;
}
int cb3(int x)
{
int st=1, dr=n, poz=-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(v[mid]>=x)
{
poz=mid;
dr=mid-1;
}
else if(v[mid]<x)
st=mid+1;
}
return poz;
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>v[i];
cin>>q;
while(q--)
{
cin>>op>>x;
if(op==0)
cout<<cb1(x)<<'\n';
else if(op==1)
cout<<cb2(x)<<'\n';
else
cout<<cb3(x)<<'\n';
}
}