Pagini recente » Cod sursa (job #1086431) | Cod sursa (job #2445455) | Cod sursa (job #2754712) | Cod sursa (job #2094137) | Cod sursa (job #2040699)
#include <bits/stdc++.h>
#define nmax 100002
using namespace std;
FILE*fin=fopen("cautbin.in","r");
ofstream fout("cautbin.out");
int n,m,v[nmax],c,x;
int cb0(int x)
{
int st=1,dr=n,mij,pmax=-1;
while(st<=dr)
{
mij=st+((dr-st)>>1);
if(v[mij]>x) dr=mij-1;
else if(v[mij]<x) st=mij+1;
else
{
pmax=mij;
st=mij+1;
}
}
return pmax;
}
int cb1(int x)
{
int st=1,dr=n,mij,pmax=-1;
while(st<=dr)
{
mij=st+((dr-st)>>1);
if(v[mij]<=x) pmax=mij, st=mij+1;
else if(v[mij]>x) dr=mij-1;
}
return pmax;
}
int cb2(int x)
{
int st=1,dr=n,mij,pmax=-1;
while(st<=dr)
{
mij=st+((dr-st)>>1);
if(v[mij]>=x) pmax=mij, dr=mij-1;
else if(v[mij]<x) st=mij+1;
}
return pmax;
}
int main()
{
fscanf(fin,"%d",&n);
int i;
for(i=1;i<=n;i++)
fscanf(fin,"%d",&v[i]);
fscanf(fin,"%d",&m);
for(i=1;i<=m;i++)
{
fscanf(fin,"%d%d",&c,&x);
if(c==0) fout<<cb0(x)<<'\n';
else if(c==1) fout<<cb1(x)<<'\n';
else fout<<cb2(x)<<'\n';
}
return 0;
}