Skip to content

Instantly share code, notes, and snippets.

@GoBigorGoHome
Created January 4, 2020 19:11
Show Gist options
  • Save GoBigorGoHome/887ca93299c97af989bb8a7b3bd3e9df to your computer and use it in GitHub Desktop.
Save GoBigorGoHome/887ca93299c97af989bb8a7b3bd3e9df to your computer and use it in GitHub Desktop.
class SegSet {
set<pii> s;
public:
// 插入闭区间[l,r]
void insert(int l, int r) {
auto iter = s.upper_bound({r, r});
while (iter != s.begin()) {
// --a,++a 运算符的优先级低于 .,-> 运算符。
if ((--iter)->second < l) {
break;
}
l = min(l, iter->first);
r = max(r, iter->second);
iter = s.erase(iter);
}
s.emplace(l, r);
}
// 闭区间[l,r]与当前集合中区间的并集是否相交?
bool intersect(int l, int r) const {
auto iter = s.upper_bound({r, r});
if (iter == s.begin()) return false;
return (--iter)->second >= l;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment