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

set的查询 - 题解

#include<bits/stdc++.h>  // 包含标准库头文件

using namespace std;    // 使用标准命名空间

int main(){
    set<int>se;
    int n,m,t;
    cin>>n>>m;
    for(int i=0;i<n;i++){
		cin>>t;
		se.insert(t);
	}
	int c,num;
	while(m--){
		cin>>c>>num;
		if(c==1){
			auto j = se.find(num);
			if(j!=se.end()) cout<<*j<<endl;
			else cout<<"NO"<<endl;
		}else if(c==2){
				auto x = se.begin();
				if(num<=*x) cout<<"NO"<<endl;
				else{
					auto j = se.lower_bound(num);
					j--;
					cout<<*j<<endl;
				}
		}else if(c==3){
			auto x = se.end();
			x--;
			if(num>=*x) cout<<"NO"<<endl;
			else{
				auto j = se.upper_bound(num);
				cout<<*j<<endl;
			}
		}		
	}
}
0 回复 0 转发 1 喜欢 2 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!