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

set的查询 - 题解

#include <bits/stdc++.h>
#define endl '\n'
using namespace std; 
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;

int n, m;
set<int> s;
inline void solve() {
    cin >> n >> m;
    for (; n-- ;) {
        int x; cin >> x;
        s.insert(x);
    }
    for (; m-- ;) {
        int a, b; cin >> a >> b;
        if (a == 1) {
            if (s.find(b) != s.end()) {
                cout << b << endl;
            }else {
                cout << "NO" << endl;
            }
        }
        if (a == 2) {
            auto idx = s.lower_bound(b);
            if (idx != s.begin()) {
                --idx;
                cout << *idx << endl;
            }else 
                cout << "NO" << endl;
        }
        if (a == 3) {
            auto id = s.upper_bound(b);
            if (id != s.end()) {
                cout << *id << endl;
            }
            else
                cout << "NO" << endl;
        }
    }
}          

int main() { 
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1; 
    //int _; cin >> _;
    while (_--) solve();
    return 0;
}
0 回复 0 转发 0 喜欢 5 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!