Pagini recente » Cod sursa (job #1721080) | Cod sursa (job #2044439) | Cod sursa (job #3247196) | Cod sursa (job #2241892) | Cod sursa (job #1649441)
#include <fstream>
#include <algorithm>
using namespace std;
int n;
int a[100005];
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f >> n;
for(int i = 1; i <= n; i ++) f >> a[i];
sort(a + 1, a + n + 1);
int m, op, x; f >> m;
int l, r;
for(; m; m --) {
f >> op >> x;
if(op == 0) {
int step = 1;
for(; step <= n; step <<= 1);
int i = 1;
for(i = 0; step; step >>= 1) {
if(i + step <= n && a[i + step] <= x)
i += step;
}
if(a[i] == x) g << i << "\n";
else g << "-1\n";
}
else if(op == 1) {
int i, step = 1;
for(; step <= n; step <<= 1);
for(i = 0; step; step >>= 1)
if(i + step <= n && a[i + step] <= x)
i += step;
g << i << "\n";
}
else if(op == 2) {
x --;
int i, step = 1;
for(; step <= n; step <<= 1);
for(i = 0; step; step >>= 1)
if(i + step <= n && a[i + step] <= x)
i += step;
g << i + 1 << "\n";
}
}
return 0;
}