ACwing(基础)--- 区间合并

区间合并

  • ①按区间左端点排序
  • ②维护一个基准(st-ed)
// 将所有存在交集的区间合并
void merge(vector<PII> &segs)
{
    vector<PII> res;

    sort(segs.begin(), segs.end());

    int st = -2e9, ed = -2e9;
    for (auto seg : segs)
        if (ed < seg.first)
        {
            if (st != -2e9) res.push_back({st, ed});
            st = seg.first, ed = seg.second;
        }
        else ed = max(ed, seg.second);

    if (st != -2e9) res.push_back({st, ed});

    segs = res;
}

ACwing(基础)--- 区间合并

上一篇:windows 2008 R2 更新补丁


下一篇:c# Mutex 互斥锁