Pagini recente » Cod sursa (job #2745382) | Cod sursa (job #791287) | Cod sursa (job #97125) | Cod sursa (job #21278) | Cod sursa (job #826135)
Cod sursa(job #826135)
#include <fstream>
#include <vector>
inline unsigned bs_lessoreq_max(unsigned what, const std::vector<unsigned> &where);
int main(){
std::ifstream fin("cautbin.in");
std::ofstream fout("cautbin.out");
unsigned N;
fin>>N;
std::vector<unsigned> sir(N);
for(unsigned i=0;i<N;++i) fin>>sir[i];
fin>>N;
while(N--){
char c;
unsigned x;
fin>>c>>x;
unsigned temp;
switch(c){
case '0': temp=bs_lessoreq_max(x,sir);
fout<<(sir[temp]==x?temp+1:-1)<<'\n';
break;
case '1': fout<<bs_lessoreq_max(x,sir)+1<<'\n'; break;
case '2': fout<<bs_lessoreq_max(x,sir)+2<<'\n';
}
}
}
inline unsigned bs_lessoreq_max(unsigned what, const std::vector<unsigned> &where){
unsigned pos=0,step=1;
for(;step<=where.size();step<<=1); step>>=1;
for(;step;step>>=1)
if(where[pos|step]<=what) pos|=step;
return pos;
}