Pagini recente » Cod sursa (job #2333198) | Cod sursa (job #3194369) | Cod sursa (job #114458) | Cod sursa (job #3235191) | Cod sursa (job #748377)
Cod sursa(job #748377)
#include<fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[100000],n,m;
void read(){
f>>n;
for(int i=0;i<n;i++)
f>>a[i];
}
int binary_search(int x){
int lo=-1,hi=n,mid;
while(hi-lo>1){
mid=(hi+lo)/2;
if(a[mid]<x)
lo=mid;
else
hi=mid;
}
return hi;
}
int main(){
read();
f>>m;
int x,y,hi;
for(int i=1;i<=m;i++){
f>>x>>y;
hi=binary_search(y);
if(x==0){
if(hi<n && a[hi]==y){
while(a[hi]==y && hi<n)
++hi;
g<<hi<<endl;
}
else
g<<"-1"<<endl;
}
if(x==1){
while(hi<n && a[hi]==y)
++hi;
g<<hi<<endl;
}
if(x==2){
while(hi>-1 && a[hi]==y)
--hi;
if(a[hi+1]==y)
g<<hi+2<<endl;
else
if(hi<n)
g<<hi+1<<endl;
else
g<<hi<<endl;
}
}
return 0;
}