range of numbers
Given an integer array of length n sorted in ascending order, and q queries.
For each query, return the start and end positions of an element k (positions are counted from 0). Returns -1 -1 if the element does not exist in the array.
input format
The first line contains integers n and q, indicating the length of the array and the number of queries.
The second line contains n integers (all in the range 1∼10000), representing the complete array. Next q lines, each line contains an integer k, representing a query element.
output format
There are q lines in total, and each line contains two integers, indicating the starting position and ending position of the element to be sought. Returns -1 -1 if the element does not exist in the array.
data range
1≤n≤100000
1≤q≤10000
1≤k≤10000
Input sample:
6 3
1 2 2 3 3 4
3
4
5
Sample output:
3 4
5 5
-1 -1
Solution
This code implements a function rangeOfNumbers that takes an integer array q, the length n of the array, and an integer x as parameters. The goal of this function is to find the subscript range of all elements in the array q that are equal to x and return the result as a string.
This function uses the binary search algorithm to find the index range of elements in the array q that are equal to x. Specifically, the function first uses the binary search algorithm to find the index l of the first element equal to x in the array q, and then uses the binary search algorithm to find the index r of the last element in the array q equal to x. Finally, the function converts the subscript range [l, r] to a string and returns it.
In the first binary search, the function uses the variables l and r to denote the left and right boundaries of the current search range. On each iteration, the function computes the index mid of the middle element and compares it with the target element x. If the middle element is greater than or equal to the target element, update the right bound r to mid, otherwise update the left bound l to mid + 1. The function ends the first binary search when the left bound l is equal to the right bound r.
In the second binary search, the function again uses the variables l and r to denote the left and right boundaries of the current search range. On each iteration, the function computes the index mid of the middle element and compares it with the target element x. If the middle element is less than or equal to the target element, update the left border l to mid, otherwise update the right border r to mid - 1. The function ends the second binary search when the left bound l is equal to the right bound r.
Finally, the function converts the subscript range [l, r] to a string and returns it. If no element equal to x exists in the array q, the function returns the string "-1 -1".
Note that the function assumes that the array q is sorted in ascending order. If the array q is unsorted, the behavior of this function is undefined.
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m;
int q[N];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> q[i];
while (m -- ) {
int x;
cin >> x;
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (q[mid] >= x) r = mid;
else l = mid + 1;
}
if (q[l] != x) cout << "-1 -1" << endl;
else {
cout << l << ' ';
l = 0, r = n - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (q[mid] <= x) l = mid;
else r = mid - 1;
}
cout << l << ' ' << endl;
}
}
return 0;
}
summary
For dichotomy, an intermediate value is found each time as the dividing line, on both sides of the value, one side satisfies a property, and the other side does not satisfy a property. Since this question is to find the range of numbers, and the sequence is increasing, you can use the dichotomy method and pay attention to the boundary conditions. When the sequence is on the right (l=mid), it should be rounded down to avoid an infinite loop