Pagini recente » Cod sursa (job #722) | Cod sursa (job #1331983)
#include <fstream>
#define Nmax 100100
using namespace std;
int Step, N, M, A[Nmax];
int upperBound(int value) {
int i, step;
for(i = 0, step = Step; step; step >>= 1)
if(i + step <= N && A[i + step] <= value)
i += step;
return (i + 1);
}
void Read(ifstream & in) {
in >> N;
for(int i = 1; i <= N; i++)
in >> A[i];
in >> M;
}
int main() {
int x, index, type;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
Read(in);
for(Step = 1; Step < N; Step <<= 1);
while(M--) {
in >> type >> x;
switch(type) {
case 0:
out << (A[index = upperBound(x) - 1] == x ? index : -1);
break;
case 1:
out << (upperBound(x) - 1);
break;
case 2:
out << (upperBound(x - 1));
}
out << '\n';
}
in.close();
out.close();
return 0;
}