Pagini recente » Cod sursa (job #27295) | Cod sursa (job #4163) | Cod sursa (job #850016) | Cod sursa (job #1687423) | Cod sursa (job #1494060)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100005;
int v[nmax], n, m, lg=1;
int bsearch1(int val)
{
int i, step;
for(i=0, step=lg; step; step=step>>1)
if(i+step<=n && v[i+step]<=val)
i=i+step;
return i;
}
int bsearch2(int val)
{
int i, step;
for(i=n, step=lg; step; step=step>>1)
if(i-step>0 && v[i-step]>=val)
i=i-step;
return i;
}
int main()
{
freopen("cautbin.in", "r", stdin);
freopen("cautbin.out", "w", stdout);
int i, p, x, poz;
scanf("%d ", &n);
for(i=1; i<=n; i++)
scanf("%d", &v[i]);
scanf("%d", &m);
while(lg<=n) lg=lg<<1;
while(m--)
{
scanf("%d %d", &p, &x);
if(p==0)
{
poz=bsearch1(x);
if(v[poz]==x) printf("%d\n", poz);
else printf("-1\n");
}
else if(p==1) printf("%d\n", bsearch1(x));
else printf("%d\n", bsearch2(x));
}
return 0;
}