Given an integer array of length n in ascending order, and q queries.
For each query, return the starting position and ending position of an element k (the position starts counting from 0).
If the element does not exist in the array, -1 -1 is returned.
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 of 1∼10000), which represents a complete array.
In the next q lines, each line contains an integer k, which represents a query element.
Output format There are
q lines in total, each line contains two integers, indicating the starting position and ending position of the element to be sought.
If the element does not exist in the array, -1 -1 is returned.
Data range
1≤n≤100000
1≤q≤10000
1≤k≤10000
Input example:
6 3
1 2 2 3 3 4
3
4
5
Sample output:
3 4
5 5
-1 -1
Time/space limit: 1s / 64MB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int num[N];
int main()
{
int n,q,k;
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}
while(q--){
scanf("%d",&k);
int l=0,r=n-1;
while(l<r){
int mid=l+r>>1;
if(k>num[mid]){
l = mid+1;
}else{
r = mid;
}
}
if(num[l]!=k){
printf("-1 -1\n");
}else{
printf("%d ",l);
int l=0,r=n-1;
while(l<r){
int mid=l+r+1>>1;
if(k<num[mid]){
r = mid-1;
}else{
l = mid;
}
}
printf("%d\n",l);
}
}
return 0;
}