Pagini recente » Cod sursa (job #1542600) | Cod sursa (job #2071498) | Cod sursa (job #2794848) | Cod sursa (job #1542285) | Cod sursa (job #2666964)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int i,n,nr,a[100001];
void caut0(int x)
{
int st,dr,mijloc;
st=1;
dr=n;
while(dr-st>1)
{
mijloc=st+(dr-st)/2;
if(a[mijloc]>x) dr=mijloc;
else st=mijloc;
}
if(x==a[st]) fout<<st<<'\n';
else if(x==a[dr])fout<<dr<<'\n';
else fout<<-1<<'\n';
}
void caut1(int x)
{
int st,dr,mijloc;
st=1;
dr=n;
while(dr-st>1)
{
mijloc=st+(dr-st)/2;
if(a[mijloc]>x) dr=mijloc;
else st=mijloc;
}
while(a[dr]>x&&dr)dr--;
fout<<dr<<'\n';
}
void caut2(int x)
{
int st,dr,mijloc;
st=1;
dr=n;
while(dr-st>1)
{
mijloc=st+(dr-st)/2;
if(a[mijloc]>=x) dr=mijloc;
else st=mijloc;
}
while(a[st]<x&&st<=n) st++;
fout<<st<<'\n';
}
int main()
{
int m;
fin>>n;
int x;
for(i=1; i<=n; i++) fin>>a[i];
fin>>m;
for(i=1; i<=m; i++)
{
fin>>nr>>x;
if(nr==0)
{
caut0(x);
}
else if(nr==1)
{
caut1(x);
}
else
{
caut2(x);
}
}
return 0;
}