首页 > 其他 > 详细

Luogu_P2161[SHOI2009]会场预约【题解】set

时间:2019-09-27 09:31:57      阅读:81      评论:0      收藏:0      [点我收藏+]

题面:https://www.luogu.org/problem/P2161

可以用set水。

先把 l , r 存在结构体里面。

重定义小于号。

struct node{
    int l,r;
    node(){}
    node(int _l,int _r){l=_l,r=_r;}
    inline bool operator < (const node &x) const {return r<x.l ;}
};

让右端点小于左端点。就是没有交。

那么find找的时候就是找a.l<b.r && b.l<a.r 。

也就是相交的。

代码如下:

#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
    int l,r;
    node(){}
    node(int _l,int _r){l=_l,r=_r;}
    inline bool operator < (const node &x) const {return r<x.l ;}
};set<node> st;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        char s[10];scanf("%s",s);
        if(s[0]==A){
            int x,y,tot=0;scanf("%d%d",&x,&y);node tp=node(x,y);
            set<node>::iterator it=st.find(tp);
            while(it!=st.end()){
                ++tot;st.erase(it);it=st.find(tp);
            }
            st.insert(tp);printf("%d\n",tot);
        }else
            printf("%lld\n",st.size());
    }
    return 0;
}

 

Luogu_P2161[SHOI2009]会场预约【题解】set

原文:https://www.cnblogs.com/ChrisKKK/p/11595672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!