Pagini recente » Cod sursa (job #2572227) | Cod sursa (job #2549773) | Cod sursa (job #547514) | Cod sursa (job #1195835) | Cod sursa (job #3302722)
#include <bits/stdc++.h>
/// Template Dutzu
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[100001],x;
int cautbin0(int st, int dr)
{
if (st==dr and v[st]==x)
return st;
else if (st==dr)
return -1;
int mij=st+(dr-st+1)/2;
if (v[mij]<=x)
cautbin0(mij,dr);
else
cautbin0(st,mij-1);
}
int cautbin1(int st, int dr)
{
if (st==dr)
return st;
int mij=st+(dr-st+1)/2;;
if (v[mij]<=x)
cautbin1(mij,dr);
else
cautbin1(st,mij-1);
}
int cautbin2(int st, int dr)
{
if (st==dr)
return st;
int mij=st+(dr-st)/2;
if (v[mij]<x)
cautbin2(mij+1,dr);
else
cautbin2(st,mij);
}
int main()
{
int n,m;
fin>>n;
for (int i=1;i<=n;i++)
fin>>v[i];
fin>>m;
for (int i=1;i<=m;i++)
{
int a;
fin>>a>>x;
if (a==0)
{
int st=1,dr=n,mij,rez=-1;
while(st<=dr)
{
mij=1LL*(st+dr)/2;
if (v[mij]<=x)
{
st=mij+1;
if (v[mij]==x)
rez=mij;
}
else
dr=mij-1;
}
cout<<rez<<'\n';
}
if (a==1)
{
int st=1,dr=n,mij,rez;
while(st<=dr)
{
mij=1LL*(st+dr)/2;
if (v[mij]<=x)
{
st=mij+1;
rez=mij;
}
else
dr=mij-1;
}
cout<<rez<<'\n';
}
if (a==2)
{
int st=1,dr=n,mij,rez;
while(st<=dr)
{
mij=1LL*(st+dr)/2;
if (v[mij]<x)
{
st=mij+1;
if (v[mij+1]==x)
rez=mij+1;
else
rez=mij;
}
else
dr=mij-1;
}
cout<<rez<<'\n';
}
}
return 0;
}