Pagini recente » Cod sursa (job #2827888) | Cod sursa (job #2975296) | Cod sursa (job #1920012) | Cod sursa (job #816927) | Cod sursa (job #795642)
Cod sursa(job #795642)
#include <fstream>
#include <algorithm>
using namespace std;
int x[100001];
int begin, end, n, mid;
bool ok;
//ofstream out ("cautbin.out", ofstream::out);
int solve2 (int y, int s) {
begin = 0;
end = n;
while (begin < end) {
mid = begin + (end - begin)/2;
if (x[mid] < s) {
begin = mid + 1;
}
else {
end = mid;
}
}
if (x[end] < s)
end++;
return end + 1;
}
int solve1 (int y, int s) {
begin = 0;
end = n;
while (begin < end) {
mid = begin + (end - begin)/2;
if (x[mid] <= s) {
begin = mid + 1;
}
else {
end = mid;
}
}
if (x[end] > s)
end--;
if (end == n)
end--;
return end + 1;
}
int solve0 (int y, int s) {
begin = 0;
end = n;
while (begin < end) {
mid = begin + (end - begin)/2;
if (x[mid] <= s) {
begin = mid + 1;
}
else {
end = mid;
}
}
if (x[end] != s && x[end-1] != s)
return -1;
if (x[end - 1] == s)
end--;
return end + 1;
}
int main () {
int q, y, s;
// ifstream in ("cautbin.in", ifstream::in);
freopen ("cautbin.in", "r", stdin);
freopen ("cautbin.out", "w", stdout);
// in >> n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
// in >> x[i];
scanf("%d", &x[i]);
}
scanf("%d", &q);
// in >> q;
sort(x + 1, x + n + 1);
while (q--)
{
//in >> y >> s;
scanf ("%d %d", &y, &s);
if (y == 0) {
int res = upper_bound(x+1,x + n +1 , s) -x-1;
if (res < n && res >= 0 && x[res] == s)
printf ("%d\n", /*solve0 (y, s)*/res );
else printf("-1\n");
}
else if (y == 1)
printf ("%d\n", /*solve1 (y, s)*/lower_bound(x + 1, x + n + 1, s + 1 ) - x - 1);
else
printf ("%d\n", /*solve2 (y, s)*/upper_bound(x + 1, x + n + 1, s - 1 ) - x);
;
}
}