class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> ret(2,-1);
if(n==0) return ret;
if(A[0]>target) return ret;
if(A[n-1]<target) return ret;
int l=0;
int r=n-1;
int left=-1;
while(l<=r)
{
int mid=(r+l)/2;
if(A[mid]==target)
{
left=mid;
break;
}
else if(A[mid]<target)
{
l=mid+1;
}
else r=mid-1;
}
if(left==-1) return ret;
int l1=0;
int l2=left-1;
while(l1<=l2)
{
int midl=(l1+l2)/2;
if(A[midl]==target)
{
l2=midl-1;
}
else l1=midl+1;
}
ret[0]=l2+1;
int r1=left+1;
int r2=n-1;
while(r1<=r2)
{
int midr=(r1+r2)/2;
if(A[midr]==target)
{
r1=midr+1;
}
else
{
r2=midr-1;
}
}
ret[1]=r1-1;
return ret;
}
};
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> ret(2,-1);
if(n==0) return ret;
if(A[0]>target) return ret;
if(A[n-1]<target) return ret;
int l=0;
int r=n-1;
int left=-1;
while(l<=r)
{
int mid=(r+l)/2;
if(A[mid]==target)
{
left=mid;
break;
}
else if(A[mid]<target)
{
l=mid+1;
}
else r=mid-1;
}
if(left==-1) return ret;
int l1=0;
int l2=left-1;
while(l1<=l2)
{
int midl=(l1+l2)/2;
if(A[midl]==target)
{
l2=midl-1;
}
else l1=midl+1;
}
ret[0]=l2+1;
int r1=left+1;
int r2=n-1;
while(r1<=r2)
{
int midr=(r1+r2)/2;
if(A[midr]==target)
{
r1=midr+1;
}
else
{
r2=midr-1;
}
}
ret[1]=r1-1;
return ret;
}
};