关于C++ STL中的lower_bound用法

  • 一、用法:

           int t=lower_bound(a+l,a+r,m)-a

  • 二、解释:

           在升序排列的a数组内二分查找[l,r)区间内的值为m的元素。返回m在数组中的下标

  • 三、特殊情况:

      1.如果m在区间中没有出现过,那么返回第一个比m大的数的下标。
      2.如果m比所有区间内的数都大,那么返回r。这个时候会越界,小心
      3.如果区间内有多个相同的m,返回第一个m的下标。
      4.时间复杂度:一次查询O(log n),n为数组长度。

        注意:m可以是pair等等。还有下标从0开始还是从1开始。

  • 四、测设代码:

#include<bits/stdc++.h>
using namespace std;
int a[1000010],t,n,l,r;
int main()
{
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	cin>>t;
	for(int i=0;i<t;i++)
		cin>>a[i];
	for(int i=0;i<t;++i)
	{
		cin>>l>>r>>n;//在[l,r)区间内查找n的位置
		cout<<lower_bound(a+l,a+r,n)-a+1;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouchenghao123/article/details/81813317
今日推荐