---
id: 1652
lang: en
translation_of: index.md
source_sha256: 129417d3d769610a89580200bb4c21410eb4dba1e229d6653a43d51dea560690
title: "[Libre OJ] 6014 [24 Network Flow Problems] Longest k-Overlapping Interval Set — Solution"
description: "Discretize the endpoints into a chain of 2n points, give each interval a capacity-1 edge whose cost is its length, and run max-cost max-flow."
tags: ["OI","Network Flow","Min-Cost Flow","Max-Cost Max-Flow"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「Libre OJ」 and 「网络流 24 题」 rendered as [Libre OJ] and [24 Network Flow Problems], matching the English editions of posts 689, 694, 729 and 1000."
  - "最长 k 可重区间集 rendered as Longest k-Overlapping Interval Set."
  - "流量 on edges rendered as capacity. 左端点 / 右端点 in the last sentence of the second paragraph kept literal as the left endpoint / the right endpoint."
---

## Main Idea

This problem is still pretty clever; I only got as far as max-cost max-flow, and after that I was stuck.

Here we present a solution with $O(n)$ edges. Consider discretizing all the endpoints into $2n$ consecutive points, then connecting each pair of adjacent endpoints $i, i+1$ with an edge of infinite capacity and zero cost. Consider the left and right endpoints of each interval: connect the left endpoint to the right endpoint, with capacity $1$ and cost equal to the length of the interval. Finally, connect the source to the left endpoint, and connect the sink to the right endpoint.

This way we can force a full flow, with the maximum cost.

<!--more-->

## Code

```cpp
// LOJ6014.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 200, INF = 0x3f3f3f3f;

int head[MAX_N], current, pre[MAX_N], dist[MAX_N];
int st, ed, flow[MAX_N], n, k, mx_range;
bool vis[MAX_N];
vector<int> vec;

struct segment
{
    int l, r;
} segs[MAX_N];

struct edge
{
    int to, weight, cost, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst, int weight, int cst)
{
    edges[current].to = dst, edges[current].weight = weight;
    edges[current].cost = cst, edges[current].nxt = head[src];
    head[src] = current++;
}

void addtube(int src, int dst, int weight, int cst)
{
    addpath(src, dst, weight, cst);
    addpath(dst, src, 0, -cst);
}

bool bfs()
{
    memset(dist, 0x3f, sizeof(dist)), memset(vis, false, sizeof(vis));
    queue<int> q;
    q.push(st), vis[st] = true, flow[st] = INF, dist[st] = 0;
    while (!q.empty())
    {
        int u = q.front();
        vis[u] = false;
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].weight > 0 && dist[edges[i].to] > dist[u] + edges[i].cost)
            {
                dist[edges[i].to] = dist[u] + edges[i].cost;
                flow[edges[i].to] = min(flow[u], edges[i].weight);
                pre[edges[i].to] = i;
                if (!vis[edges[i].to])
                    vis[edges[i].to] = true, q.push(edges[i].to);
            }
    }
    return dist[ed] != INF;
}

int mcmf()
{
    long long ans = 0;
    while (bfs())
    {
        int p = ed, i = pre[p];
        ans += 1LL * flow[ed] * dist[ed];
        while (p != st)
        {
            edges[i].weight -= flow[ed], edges[i ^ 1].weight += flow[ed];
            p = edges[i ^ 1].to, i = pre[p];
        }
    }
    return ans;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &segs[i].l, &segs[i].r);
        if (segs[i].l > segs[i].r)
            swap(segs[i].l, segs[i].r);
        vec.push_back(segs[i].l), vec.push_back(segs[i].r);
    }
    vec.push_back(-0x3f3f3f3f);
    sort(vec.begin(), vec.end());
    st = 0, ed = 2 * n + 1;

    addtube(st, 1, k, 0);
    for (int i = 1; i < 2 * n; i++)
        addtube(i, i + 1, INF, 0);
    addtube(2 * n, ed, k, 0);

    for (int i = 1; i <= n; i++)
    {
        int len = segs[i].r - segs[i].l;
        segs[i].l = lower_bound(vec.begin(), vec.end(), segs[i].l) - vec.begin();
        segs[i].r = lower_bound(vec.begin(), vec.end(), segs[i].r) - vec.begin();
        addtube(segs[i].l, segs[i].r, 1, -len);
    }
    printf("%d", -mcmf());
    return 0;
}
```
