返回题解分享
讨论 / 题解分享/ 帖子详情

set的查询 - 题解

#include <iostream>
#include <map>
#include <set>
using namespace std;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

int n, m, t;
cin >> n >> m;
set<int> s;

for (int i = 0; i < n; i++)
{
    cin >> t;
    s.insert(t);
}
int a, b;
while (m--)
{
    cin >> a >> b;
    if (a == 1)
    {
        if (s.find(b) == s.end())
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << b <<endl;
        }
    }
    if (a == 2)
    {
        set<int> ::iterator it;
        it = s.lower_bound(b);
        if (it != s.begin())
        {
            cout << *(--it) << endl;
        }
        else
        {
            cout << "NO\n";
        }
    }
    if (a == 3)
    {
        set<int> ::iterator it;
        it = s.upper_bound(b);
        if (it != s.end())
        {
            cout << *it << endl;
        }
        else
        {
            cout << "NO\n";
        }
    }
}
return 0;


}
0 回复 0 转发 0 喜欢 3 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!