数的范围-golang
blablabla
数的范围-golang
package main
import "fmt"
var n, q int
var arr []int
func main() {
fmt.Scanf("%d%d", &n, &q)
arr = make([]int, n)
for i := 0; i < n; i++ {
fmt.Scanf("%d", &arr[i])
}
for i := 0; i < q; i++ {
var k int
fmt.Scanf("%d", &k)
search(arr, k)
}
}
func search(arr []int, k int) {
l, r := 0, len(arr)-1
for l < r {
mid := (l + r) >> 1
if arr[mid] >= k {
r = mid
continue
}
l = mid+1
}
if arr[l] != k {
fmt.Printf("%d %d\n", -1, -1)
return
}
fmt.Printf("%d ", l)
l, r = 0, len(arr)-1
for l < r {
mid := (l + r + 1) >> 1
if arr[mid] <= k{
l = mid
continue
}
r = mid-1
}
fmt.Printf("%d\n", r)
}