会场预约

题目链接:https://www.luogu.com.cn/problem/P2161

题意:

若干个a操作和b操作

a操作:给定l,r,删除线段中与[l,r]有交集的区间,输出删除的区间个数,并且[l,r]进入线段

b操作:输出线段有多少个区间

思路:

看到题解写的STL写法,感到惊奇。

定义结构体node{l,r},其中小于运算符重载this.r<a.l

此时如果一个区间A完全在另一个区间B左侧/右侧,那么严格A<B/A>B

如果有交集,那么A=B

然后不断用set的find方法去重

struct node{
    int l,r;
    bool operator<(const node&a)const{
        return r<a.l;
    }
};
set<node>st;
void solve(){
    int n;cin>>n;
    rep(i,1,n){
        char opt;cin>>opt;
        if(opt=='A'){
            node now;
            cin>>now.l>>now.r;
            auto it=st.find(now);
            int cnt=0;
            while(it!=st.end()){
                cnt++;
                st.erase(it);
                it=st.find(now);
            }
            st.insert(now);
            cout<<cnt<<endl;
        }else{
            cout<<st.size()<<endl;
        }
    }
}
posted @ 2025-05-01 19:11  Marinaco  阅读(17)  评论(0)    收藏  举报
//雪花飘落效果