Pagini recente » Cod sursa (job #1626869) | Cod sursa (job #252079)
Cod sursa(job #252079)
#include<fstream.h>
ifstream f("cautbin.in");
ofstream g("cautbin.out");
long x1,x2,s,ok,a[100000],n,i,m;
int cautbin(int st,int dr,int x)
{
int aux=st+(dr-st)/2;
if(dr-st<2) return st;
if(x>=a[aux]) st=aux;
else if(x<=a[aux]) dr=aux;
cautbin(st,dr,x);
}
int main()
{
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
f>>m;
for(i=1;i<=m;i++)
{
f>>x1>>x2;
if(x1==0)
{
ok=0;
s=cautbin(1,n,x2);
if(a[s]==x2) g<<s<<"\n",ok=1;
else if(a[s+1]==x2) g<<(s+1)<<"\n",ok=1;
if(ok==0) g<<-1<<"\n";
}
if(x1==1)
{
s=cautbin(1,n,x2);
while(a[s+1]<x2) s++;
g<<s<<"\n";
}
if(x1==2)
{
s=cautbin(1,n,x2);
s++;
while(a[s-1]>x2) s--;
g<<s<<"\n";
}
}
return 0;
}