lgP2161 会场预约

在数轴上维护一个线段集合S,支持两种操作:

  • A l r:将S中所有与线段[l,r]相关的线段删除,并将[l,r]加入S中,需要输出删除个数。
  • B:查询S中元素数量。

分析:重载小于号,如果x小于y,那么x严格小于y,即x的右端点要小于y的左端点,这样定义相交的线段都是相等的,可以用set来找。

#include <bits/stdc++.h>
using llong = long long;

struct Info {
    int l, r;
    bool operator<(const Info &rhs) const {
        return r < rhs.l;
    }
};

void solve() {
    std::set<Info> st;
    int n;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string op;
        std::cin >> op;
        if (op == "A") {
            Info info;
            std::cin >> info.l >> info.r;
            int cnt = 0;            
            auto it = st.find(info);
            while (it != st.end()) {
                cnt += 1;
                st.erase(it);
                it = st.find(info);
            }
            st.insert(info);
            std::cout << cnt << "\n";
        } else if (op == "B") {
            std::cout << st.size() << "\n";
        }
    }
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}
posted @ 2024-07-14 18:09  chenfy27  阅读(1)  评论(0编辑  收藏  举报