Pagini recente » Cod sursa (job #3320724) | Cod sursa (job #1408604) | Cod sursa (job #3314263) | Cod sursa (job #3313437) | Cod sursa (job #3349986)
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100001];
int main()
{
int n,m,i;
fin >> n;
for (i=1;i<=n;i++)
{
fin >> v[i];
}
fin >> m;
for (i=1;i<=m;i++)
{
int tip,x;
fin >> tip >> x;
if (tip==0)
{
int pozmax=-1,st=1,dr=n;
while (st<=dr)
{
int mijloc=(st+dr)/2;
if (v[mijloc]>x)
{
dr=mijloc-1;
}
else if (v[mijloc]==x)
{
st=mijloc+1;
pozmax=mijloc;
}
else if (v[mijloc]<x)
{
st=mijloc+1;
}
}
fout << pozmax << '\n';
}
if (tip==1)
{
int pozmax=-1,st=1,dr=n;
while (st<=dr)
{
int mijloc=(st+dr)/2;
if (v[mijloc]>x)
{
dr=mijloc-1;
}
else if (v[mijloc]<=x)
{
st=mijloc+1;
pozmax=mijloc;
}
}
fout << pozmax << '\n';
}
if (tip==2)
{
int pozmax=-1,st=1,dr=n;
while (st<=dr)
{
int mijloc=(st+dr)/2;
if (v[mijloc]>=x)
{
dr=mijloc-1;
pozmax=mijloc;
}
else if (v[mijloc]<x)
{
st=mijloc+1;
}
}
fout << pozmax << '\n';
}
}
return 0;
}