给定一个初始为空的可重复集合。接下来执行若干次询问,询问有六种类型:
op=1,表示向集合中添加数值 x
op=2,表示删除集合中数值为 x 的一个元素,若不存在则忽略该操作
op=3,表示询问集合中,最小的值是多少
op=4,表示询问集合中,最大的值是多少
op=5,表示询问集合中,x 的前驱,即最大的 <x 的数
op=6,表示询问集合中,x 的后继,即最小的 >x 的数
#include <bits/stdc++.h>
using namespace std;
multiset<int> tree;
void add(int x)
{
tree.insert(x);
}
void remove(int x)
{
tree.erase(x);
}
int getBig()
{
return *tree.begin();
}
int getSmall()
{
return *prev(tree.end());
}
int getPredecessor(int x)
{
multiset<int>::iterator it = tree.lower_bound(x);
if (it == tree.begin()) return -1;
--it;
return *it;
}
int getSuccessor(int x)
{
multiset<int>::iterator it = tree.upper_bound(x);
if (it == tree.end()) return -1;
return *it;
}
int main()
{
int q,op,x;
cin >> q;
for(int i = 1;i <= q;i++)
{
cin >> op;
if(op == 1)
{
cin >> x;
add(x);
}
if(op == 2)
{
cin >> x;
remove(x);
}
if(op == 3) cout << getBig() << endl;
if(op == 4) cout << getSmall() << endl;
if(op == 5)
{
cin >> x;
cout << getPredecessor(x) << endl;
}
if(op == 6)
{
cin >> x;
cout << getSuccessor(x) << endl;
}
}
return 0;
}
本地样例通过,然而却WA了,能解释一下吗