Pagini recente » Clasament dupa rating | Rating Rotariu George (george-rotariu) | Cod sursa (job #1646355) | Monitorul de evaluare | Cod sursa (job #900979)
Cod sursa(job #900979)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<bitset>
#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define maxn 100010
using namespace std;
int a[maxn],op,key,n,i,x,m;
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&n);
for(i=1; i<=n; ++i)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&op,&key);
if(op == 0)
{
x = upper_bound(a+1, a+n+1, key)-a-1;
if(x>0 && x<=n && a[x] == key)
printf("%d\n",x);
else
printf("-1\n");
}
else if(op == 1)
{
printf("%d\n",lower_bound(a+1,a+n+1,key+1)-a-1);
}
else
printf("%d\n",upper_bound(a+1,a+n+1,key-1)-a);
}
return 0;
}