Pagini recente » Cod sursa (job #2315200) | Cod sursa (job #281521) | Cod sursa (job #2106290) | Cod sursa (job #2859819) | Cod sursa (job #619765)
Cod sursa(job #619765)
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
vector <int> a;
int n,m;
int main()
{
int index,i,t,x;
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&t);
a.push_back(t);
}
scanf("%d",&m);
for (i=0;i<m;i++)
{
scanf("%d %d",&t,&x);
if (t==0)
{
index=(int)(upper_bound(a.begin(),a.end(),x)-a.begin()); // if(a[i]>x) return i; with starting index of 1
if (a[index-1]==x)
printf("%d\n",index);
else
printf("-1\n");
}
if (t==1)
{
index=(int)(upper_bound(a.begin(),a.end(),x)-a.begin());
printf("%d\n",index);
}
if (t==2)
{
index=(int)(lower_bound(a.begin(),a.end(),x)-a.begin()); // if(a[i]<=x) return i; with starting index of 1
printf("%d\n",index+1);
}
}
return 0;
}