abc 241 D.Sequence Query


Sequence Query

题意:

插入元素,查找不小于x的第k个元素和查找不大于x的第k个元素

思路:

使用multiset来写,排序复杂度为nlogn

注意这里的upper和lower,也可以直接用se.lower_bound()来表示或者upper_bound()来表示,迭代器用auto

upper时找到大于x的第一个元素坐标为it,然后范围是[ it-k,it-1 ],所以这里it先--

所以第一种情况我们需要限制 **it **的范围

而lower_bound找到的是大于等于x的第一个元素,查找区间范围是[ it,it+k]所以it++可以留在后手,而循环的i其实不影响 都用[ 0,k)

#include
using namespace std;
typedef multiset IntSet;
IntSet se;
signed main()
{
	int q;
	cin>>q;
	long long n,x,k;
	for(int i=1;i<=q;i++)
	{
		cin>>n>>x;
		if(n==1)
		{
			se.insert(x);
		}
		else if(n==2)
		{
			cin>>k;
			int cnt=0;
			auto it = se.upper_bound(x);
			if(it==se.begin())	//这里一定要千万注意!!!先限制范围
			{
				cout<<-1<>k;
			auto it = se.lower_bound(x);
			for(int i=0;i<=k-1;i++)
			{	
				if(it==se.end())
				{
					cout<<-1<