Pagini recente » Cod sursa (job #2091470) | Cod sursa (job #121211) | Monitorul de evaluare | Istoria paginii runda/simulare-cartita-22/clasament | Cod sursa (job #2666599)
#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=0;
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 fout<<-1<<'\n';
}
void caut1(int x)
{
int st,dr,mijloc;
st=0;
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)st--;
fout<<st<<'\n';
}
void caut2(int x)
{
int st,dr,mijloc;
st=0;
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;
}