Pagini recente » Cod sursa (job #1104746) | Cod sursa (job #1698482) | Cod sursa (job #1028979) | Cod sursa (job #2243479) | Cod sursa (job #2795488)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100005],n,m,p=1;
int bis(int x,int bound){
int i=0,l=0;
if(bound==1)
for(i=0,l=p;l>0;l>>=1){
if(i+l<=n && v[i+l]<=x)
i+=l;}
else if(bound==0){
for(i=n,l=p;l>0;l>>=1){
if(i-l>=1 && v[i-l]>=x)
i-=l;
}
}
return i;
}
int main()
{
int c,x,pos;
f>>n;
while(p<n)
p<<=1;
for(int i=1;i<=n;i++){
f>>v[i];
}
f>>m;
for(int i=1;i<=m;i++){
f>>c>>x;
if(c==0){
pos=bis(x,1);
if(v[pos]!=x)
g<<"-1\n";
else g<<pos<<"\n";
}
else if(c==1) g<<bis(x,1)<<"\n";
else if(c==2) g<<bis(x,0)<<"\n";
}
}