[洛谷P2161][SHOI2009]会场预约

题目大意:有两种操作:

  1. $A\;l\;r:$表示加入区间$[l,r]$,并把与之冲突的区间删除,输出删除的区间的个数,区间$A$于区间$B$冲突当且仅当$A\cap B\not=\varnothing$
  2. $B:$输出现在有几个区间

题解:用$STL$的$set$,定义区间$A<B$为$A.r<B.l$,这样寻找冲突的区间只需要在$set$中$find$即可,注意删除后$set$指针不可用,需要重新找

卡点:

 

C++ Code:

#include <cstdio>
#include <set>
int n;
struct Interval {
	int l, r;
	inline Interval() {}
	inline Interval(int __l, int __r) : l(__l), r(__r) {}
	inline friend bool operator < (const Interval &lhs, const Interval &rhs) {
		return lhs.r < rhs.l;
	}
} ;
std::set<Interval> S;
int main() {
	scanf("%d", &n);
	while (n --> 0) {
		char op;
		scanf("%1s", &op);
		if (op == 'A') {
			int a, b, res = 0;
			scanf("%d%d", &a, &b);
			Interval now(a, b);
			std::set<Interval>::iterator it = S.find(now);
			while (it != S.end()) {
				S.erase(it);
				res++;
				it = S.find(now);
			}
			S.insert(now);
			printf("%d\n", res);
		} else printf("%llu\n", S.size());
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/10172567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值