算法模板---区间合并
区间合并
统计区间中的整数数目
解析
利用set反向存储区间,利于通过lower_bound()找到右端点大于等于left-1的区间
通过l,r合并区间,不断删除与之相交的区间
my'code
class CountIntervals {
public:
typedef pair PII;
set heap;
int ans=0;
CountIntervals() {
}
void add(int left, int right) {
int l=left,r=right;
auto it = heap.lower_bound(PII(left-1,-2e9));
while(it!=heap.end()){
if(it->second>right+1)break;
l=min(l,it->second);
r=max(r,it->first);
ans-=it->first-it->second+1;
heap.erase(it++);
}
ans+=r-l+1;
heap.insert(PII(r,l));
}
int count() {
return ans;
}
};
/**
* Your CountIntervals object will be instantiated and called as such:
* CountIntervals* obj = new CountIntervals();
* obj->add(left,right);
* int param_2 = obj->count();
*/
模板
class section {
public:
using pii = pair;
set s;//反向存储,{right, left} == [left, right)
uint64_t count;
section(uint64_t n = 0) :count(n) {}
int insert(const pii& x) { // 区间 [left, right)
if (x.first >= x.second)return false;
uint64_t before = count;
int addright = x.second, addleft = x.first;
for (auto p = s.lower_bound({ x.first, INT_MIN }); p != s.end() && p->second <= x.second; ) {
addright = max(addright, p->first);
addleft = min(addleft, p->second);
count -= p->first - p->second;
p = s.erase(p);
}
count += addright - addleft;
s.insert({ addright, addleft });
return count > before;
}
};
class CountIntervals {
public:
section s;
CountIntervals() {
}
void add(int left, int right) {
s.insert({left, right + 1});
}
int count() {
return s.count;
}
};
作者:MuriyaTensei
链接:https://leetcode.cn/problems/count-integers-in-intervals/solution/c-by-muriyatensei-b913/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。