Pagini recente » Cod sursa (job #2566907) | Cod sursa (job #64506) | Cod sursa (job #220327) | Cod sursa (job #536109) | Cod sursa (job #1658440)
#include <fstream>
#include <algorithm>
using namespace std;
int a[100003], n;
int main ()
{
int m, x, y, i, t;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin >> n;
for (i = 1; i <= n; ++ i)
fin >> a[i];
sort(a + 1, a + n + 1);
fin >> m;
for (i = 1; i <= m; ++ i)
{
fin >> t >> y;
if (t == 0)
{
x = upper_bound(a + 1, a + n + 1, y) - a - 1;
if (x <= n && x >= 1 && a[x] == y)
fout << x << "\n";
else
fout << "-1\n";
}
else if (t == 1)
{
x = lower_bound(a + 1, a + n + 1, y + 1) - a - 1;
fout << x << "\n";
}
else
{
x = upper_bound(a + 1, a + n + 1, y - 1) - a;
fout << x << "\n";
}
}
fin.close();
fout.close();
}