算法模板---区间合并

区间合并

统计区间中的整数数目

解析#

利用set反向存储区间,利于通过lower_bound()找到右端点大于等于left-1的区间
通过l,r合并区间,不断删除与之相交的区间

my'code#

class CountIntervals {
public:
    typedef pair<int,int> PII;
    set<PII> 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<int, int>;
	set<pii> 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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @   lxp_blog  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
主题色彩