int bsearch(int a[], int n, int v) { int left = 0, right = n; while(left < right) { int mid = left + right >> 1; if (a[mid] <= v) left = mid + 1; else right = mid; } return left - 1; }