D - Sequence Query
思路:
m
u
l
t
i
s
e
t
multiset
multiset 模拟
假设里边元素为
3
,
4
3, 4
3,4
auto it = se.lower_bound(4);
cout << *it << endl;// 输出 4
--it;
cout << *it << endl;// 输出 3
--it;
cout << *it << endl;// 输出 3
--it;
cout << *it << endl;// 输出 3
指针左移到最左端后,再移动也不会越界,而且因为集合可以有重复元素,难以判断小于
x
x
x 的元素个数是否
>
=
k
>=k
>=k。
右移类似
因此需要初始化
m
u
l
t
i
s
e
t
multiset
multiset 里的元素,一开始塞入
5
5
5 个
−
1
-1
−1 和
5
5
5 个
i
n
f
inf
inf
这样就可以判断
−
1
-1
−1 了
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
void work()
{
int q;cin >> q;
multiset <ll> se;
for(int i = 0; i < 5; ++i) se.insert(-1), se.insert(9e18);
while(q--){
int op;cin >> op;
if(op == 1){
ll x;cin >> x;se.insert(x);
}
else if(op == 2){
ll x, k;cin >> x >> k;
--k;
auto it = se.upper_bound(x);
--it;// 指针移到x元素的最后一个
while(k--) --it;
cout << *it << endl;
}
else {
ll x, k;cin >> x >> k;
--k;
auto it = se.lower_bound(x);
while(k--) ++it;
if(*it == 9e18) cout << -1 << endl;
else cout << *it << endl;
}
}
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}