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

题目大意:有两种操作:

  1. $A;l;r:$表示加入区间$[l,r]$,并把与之冲突的区间删除,输出删除的区间的个数,区间$A$于区间$B$冲突当且仅当$Acap B ot=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
", res);
		} else printf("%llu
", S.size());
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/10172567.html