Pagini recente » Cod sursa (job #822001) | Cod sursa (job #2211690) | Cod sursa (job #271969) | Cod sursa (job #2502812) | Cod sursa (job #2269797)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,v[100005];
void read()
{
int i;
fin>>n;
for(i=1;i<=n;i++)
{
fin>>v[i];
}
}
int CB0(int x)
{
int st,dr,mij,ans=-1;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
ans=mij;
st=mij+1;
}
else if(v[mij]>x)
{
dr=mij-1;
}
else st=mij+1;
}
return ans;
}
int CB1(int x)
{
int st,dr,mij,ans;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
{
ans=mij;
st=mij+1;
}
else dr=mij-1;
}
return ans;
}
int CB2(int x)
{
int st,dr,mij,ans;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=x)
{
ans=mij;
dr=mij-1;
}
else st=mij+1;
}
return ans;
}
void solve()
{
int i,tip,nr;
fin>>m;
while(m--)
{
fin>>tip>>nr;
if(tip==0)
{
fout<<CB0(nr)<<"\n";
}
else if(tip==1)
{
fout<<CB1(nr)<<"\n";
}
else fout<<CB2(nr)<<"\n";
}
}
int main()
{
read();
solve();
}