【stl的神奇操作】用集合搞定区间相交

https://www.luogu.com.cn/problem/P2161

根据大佬说的来做了。

判断结构体a和b大小,看a是否完全在b的左边,一下代码中,相交的集合是在数学意义上相同的。

例如1--3 等于  2  -- 5

相交就相等

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
struct Node{
	int l,r;
}; 
bool operator < (const Node a,const Node b){
	return a.r < b.l;
}

set<Node>ins;
string op;
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		cin>>op;
		if(op == "A"){
			int l,r;
			scanf("%d %d",&l,&r);
			Node ans ;
			ans.l = l;
			ans.r = r;
			set<Node>::iterator it = ins.find(ans);
			int cnt=0;
			while(it != ins.end()){
				cnt++;
				ins.erase(it);
				it = ins.find(ans);
			}
			ins.insert(ans);
			printf("%d
",cnt);
		}
		else{
			printf("%d
",ins.size());
		}
		cout<<endl;
	} 
	return 0;
} 
寻找真正的热爱
原文地址:https://www.cnblogs.com/lesning/p/12346658.html