这道题的意思很容易理解 这里会使用到c++ set中的lower_bound和upper_bound来帮助我们寻找需要的值的范围
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int m,k;
cin>>m>>k;
set<int> op;//有序不重合
while(m--){
string s;
int x;
cin>>s>>x;
//add
if(s=="add"){
//先找到op中比x-k大且最接近的一个属
auto it=op.lower_bound(x-k);
//这个数不存在 或 大于x+k就说明符合插入的条件
if(it==op.end() || *it>x+k){
op.insert(x);
}
}
//del 删除|op-x|<=k;
else if (s=="del") {
auto l=op.lower_bound(x-k);
auto r=op.upper_bound(x+k);
op.erase(l,r);
}
//query
else if (s=="query") {
auto it=op.lower_bound(x-k);
if(it!=op.end() && *it <= x+k){
cout<<"Yes"<<endl;
}else cout<<"No"<<endl;
}
}
}

京公网安备 11010502036488号