bool first = 1;
int lst;
unsigned lstx;
int l, r;
int binary_search(const unsigned *a, int n, unsigned x)
{
if (first)
{
first = 0;
l = 0, r = n - 1;
}
else
{
if (x == lstx) return lst;
else if (x > lstx) l = lst + 1, r = n - 1;
else l = 0, r = lst - 1;
}
lstx = x;
while (l <= r)
{
int mid = l + r >> 1;
if (a[mid] == x) return lst = mid;
else if (a[mid] > x) l = mid + 1;
else r = mid - 1;
}
return lst;
}