Pagini recente » Cod sursa (job #1853357) | Cod sursa (job #49994) | Cod sursa (job #1784722) | Cod sursa (job #1510986) | Cod sursa (job #2803507)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100005], n, m, a, x, i;
int a0 (int v[], int n, int x)
{
int st=1, dr=n, m, poz=0;
while (st<=dr)
{
m=(st+dr)/2;
if (v[m]<=x)
{
if (v[m]==x) poz=m;
st=m+1;
}
else
dr=m-1;
}
if (poz==0) return -1;
return poz;
}
int a1 (int v[], int n, int x)
{
int st=1, dr=n, m, poz=0;
while (st<=dr)
{
m=(st+dr)/2;
if (v[m]<=x)
{
poz=m;
st=m+1;
}
else
dr=m-1;
}
return poz;
}
int a2 (int v[], int n, int x)
{
int st=1, dr=n, m, poz=0;
while (st<=dr)
{
m=(st+dr)/2;
if (v[m]>=x)
{
poz=m;
dr=m-1;
}
else
st=m+1;
}
return poz;
}
int main()
{
fin >> n;
for (i=1; i<=n; i++)
fin >> v[i];
fin >> m;
for (i=1; i<=m; i++)
{
fin >> a >> x;
if (a==0)
fout << a0(v, n, x) << '\n';
if (a==1)
fout << a1(v, n, x) << '\n';
if (a==2)
fout << a2(v, n, x) << '\n';
}
return 0;
}