Pagini recente » Cod sursa (job #2477278) | Cod sursa (job #2401908) | Cod sursa (job #915048) | Cod sursa (job #1259704) | Cod sursa (job #2070099)
#include <bits/stdc++.h>
using namespace std;
int v[100001];
int binarySearch (int x, int n)
{
int position = 0;
for (int power = 20; power >= 0; --power)
if (position + (1 << power) <= n && v[position + (1 << power)] <= x)
position += (1 << power);
return position;
}
int solveOneQuery (int type, int x, int n)
{
if (type == 0)
return (v[binarySearch (x, n)] == x ? binarySearch(x, n) : -1);
if (type == 1)
return binarySearch (x, n);
return binarySearch (x - 1, n) + 1;
}
int main(int argc, char const *argv[])
{
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n, m;
fin >> n;
for (int i = 1; i <= n; ++i)
fin >> v[i];
fin >> m;
for (int i = 1; i<= m; ++i)
{
int type, x;
fin >> type >> x;
fout << solveOneQuery (type, x, n) << '\n';
}
return 0;
}