Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #120790) | Cod sursa (job #2147568) | Cod sursa (job #796566)
Cod sursa(job #796566)
#include <iostream>
#include <fstream>
using namespace std;
ifstream ifs("cautbin.in");
ofstream ofs("cautbin.out");
int *a;
int zero(int x,int min,int max);
int one(int x,int min,int max);
int two(int x,int min,int max);
int main(){
int N,M;
ifs>>N;
a=new int[N];
for(int i=0;i<N;i++) ifs>>a[i];
ifs>>M;
for(int i=0;i<M;i++){
int f,x;
ifs>>f>>x;
if(f==0)
zero(x,0,N);
else if(f==1)
one(x,0,N);
else
two(x,0,N);
}
}
inline int zero(int x,int min,int max){
int pivot;
while(max>=min){
pivot=(max+min)/2;
if(x>a[pivot])
min=pivot+1;
else if(x<a[pivot])
max=pivot-1;
else{
int tmp=a[pivot];
while(a[pivot]==tmp)
pivot++;
ofs<<pivot<<endl;
return 0;
}
}
ofs<<-1<<endl;
return 0;
}
inline int one(int x,int min,int max){
int pivot;
while(max>=min){
pivot=(max+min)/2;
if(x>a[pivot])
min=pivot+1;
else if(x<a[pivot])
max=pivot-1;
else{
int tmp=a[pivot];
while(a[pivot]==tmp)
pivot++;
ofs<<pivot<<endl;
return 0;
}
}
ofs<<pivot+1<<endl;
return 0;
}
inline int two(int x,int min,int max){
int pivot;
while(max>=min){
pivot=(max+min)/2;
if(x>a[pivot])
min=pivot+1;
else if(x<a[pivot])
max=pivot-1;
else{
int tmp=a[pivot];
while(a[pivot]==tmp)
pivot--;
ofs<<pivot+2<<endl;
return 0;
}
}
ofs<<pivot+1<<endl;
return 0;
}