指纹锁(重载set函数)

标题 指纹锁

题目链接:指纹锁

考点:重载set

题意:进行多次插入,删除,查询工作,每次操作都是针对一个区块

思路:

  • 如果直接用map+快读的话会tle
  • set的特点就是里面已经有了的不会再加入,但是没有针对一个区块所有我们需要对set进行重载

代码部分:

#include <iostream>
#include <string>
#include <set>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const int MAX = 1e6 + 7;
int m, k;
struct cmp {
    bool operator () (const int& u, const int& v) const {
        if (abs(u - v) <= k) return false;//判断是否属于一个区块
        return u < v;//同时排序
    }
};
set<int, cmp> se;

int main() {
    IOS;
    cin >> m >> k;
    while (m--) {
        string s; cin >> s;
        int x; cin >> x;
        if ("add" == s) se.insert(x);
        else if ("del" == s) se.erase(x);
        else
            if (se.find(x) != se.end()) cout << "Yes" << endl;
            else cout << "No" << endl;
    }
    return 0;
}
posted @ 2023-11-12 20:24  锦林不睡觉  阅读(22)  评论(0)    收藏  举报