题目 › usaco-1526

Package Pickup

USACO 2025 US Open · 铂金组 · 2025-03 · L4 顶尖 · C++17 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

**Note: The time limit for this problem is 4s, 2x the default.**

Farmer John has distributed cows and packages in a weird pattern across the number line using the following process:

  • Farmer John chooses a number $M$ ($1 \le M \le 10^{18}$).
  • Farmer John chooses $N$ ($1 \le N \le 2 \cdot 10^4)$ intervals $[L_i, R_i]$ to distribute cows in ($1 \le L_i \le R_i \le 10^{18}$). He then places cows at locations $L_i, L_i + M, L_i + 2M, \ldots, R_i$. It is guaranteed that $R_i - L_i$ is a multiple of $M$.
  • Farmer John chooses $P$ ($1 \le P \le 2 \cdot 10^4)$ intervals $[A_i, B_i]$ to distribute packages in ($1 \le A_i \le B_i \le 10^{18}$). He then places packages at locations $A_i, A_i + M, A_i + 2M, \ldots, B_i$. It is guaranteed that $B_i - A_i$ is a multiple of $M$.
Once the cows and packages are distributed, Farmer John wants to see how long it takes the cows to pick up the packages. Every second, Farmer John can issue a command to a single cow to move one unit left or right of their current position with his handy walkie talkie. If a cow travels to the position where a package is located, they are able to pick it up. Farmer John wants to know the minimum time in seconds that it would take the cows to pick up every package.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $M$, $N$, and $P$.

The next $N$ lines each contain two integers $L_i$ and $R_i$.

The next $P$ lines each contain two integers $A_i$ and $B_i$.

OUTPUT FORMAT (print output to the terminal / stdout):

Output a single integer, representing the minimum amount of time it can take the cows to pick up all the packages, given that every second, he can issue a single left/right command to a single cow.

SAMPLE INPUT:

100 3 7
10 10
20 20
30 30
7 7
11 11
13 13
17 17
24 24
26 26
33 33

SAMPLE OUTPUT:

22
In the above test case, suppose the cows and packages are numbered from left to right. Farmer John can follow this procedure to pick up the packages in 22 seconds:
  • Issue $3$ lefts to cow $1$ so that it picks up package $1$
  • Issue $3$ rights to cow $3$ so that it picks up package $7$
  • Issue $4$ rights to cow $2$ so that it picks up package $5$
  • Issue $10$ rights to cow $1$ so that it picks up packages $2$, $3$, and $4$
  • Issue $2$ rights to cow $2$ so that it picks up package $6$

SAMPLE INPUT:

2 1 1
1 5
2 6

SAMPLE OUTPUT:

3

There are three cows and three packages. Farmer John can issue one right to each cow.

SCORING:

  • Input 3-4: It is guaranteed that the total number of cows and packages does not exceed $2 \cdot 10^5$
  • Inputs 5-10: It is guaranteed that $N, P \le 500$.
  • Inputs 11-13: It is guaranteed that no intervals of packages or cows intersect.
  • Inputs 14-20: No additional constraints.

Problem credits: Suhas Nagar and Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 65.7k平均输出 token(含思考) ↓
  • 22.5输出速度 tokens/s ↑
  • 2941 秒延迟 ↓
  • 0.35×CPU 时间 / 官方解 ↓
  • 同阶 2、更优 1时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 5.0采样 3 · 答案错误
  • 64.3k平均输出 token(含思考) ↓
  • 21.8输出速度 tokens/s ↑
  • 2941 秒延迟 ↓
  • 0.32×CPU 时间 / 官方解 ↓
  • 同阶 2时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 20.0采样 2 · 运行错误
  • 10.0采样 3 · 答案错误
  • 100.8k平均输出 token(含思考) ↓
  • 125.8输出速度 tokens/s ↑
  • 872 秒延迟 ↓
  • 0.39×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 0.0采样 2 · 编译错误
  • 100采样 3 · 解出
  • 112.8k平均输出 token(含思考) ↓
  • 144.8输出速度 tokens/s ↑
  • 936 秒延迟 ↓
  • 0.12×CPU 时间 / 官方解 ↓
  • 同阶 1、更优 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 402 行
#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
using i128 = __int128_t;

const i128 INF = (i128)1 << 120;

struct Mat {
    i128 a[2][2];
};

Mat identityMat() {
    Mat r;
    r.a[0][0] = r.a[1][1] = 0;
    r.a[0][1] = r.a[1][0] = INF;
    return r;
}

Mat zeroMat() {
    Mat r;
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            r.a[i][j] = 0;
    return r;
}

bool isIdentity(const Mat &m) {
    return m.a[0][0] == 0 && m.a[1][1] == 0 &&
           m.a[0][1] >= INF / 2 && m.a[1][0] >= INF / 2;
}

Mat multiply(const Mat &a, const Mat &b) {
    if (isIdentity(a)) return b;
    if (isIdentity(b)) return a;
    Mat r;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            r.a[i][j] = INF;
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int k = 0; k < 2; k++) {
            if (a.a[i][k] >= INF / 2) continue;
            for (int j = 0; j < 2; j++) {
                if (b.a[k][j] >= INF / 2) continue;
                r.a[i][j] = min(r.a[i][j], a.a[i][k] + b.a[k][j]);
            }
        }
    }
    return r;
}

Mat powerMat(Mat a, int64 e) {
    Mat r = identityMat();
    while (e > 0) {
        if (e & 1) r = multiply(r, a);
        e >>= 1;
        if (e) a = multiply(a, a);
    }
    return r;
}

void applyMat(const Mat &m, i128 &A, i128 &B) {
    i128 na = INF, nb = INF;
    if (A < INF / 2) {
        if (m.a[0][0] < INF / 2) na = min(na, A + m.a[0][0]);
        if (m.a[0][1] < INF / 2) nb = min(nb, A + m.a[0][1]);
    }
    if (B < INF / 2) {
        if (m.a[1][0] < INF / 2) na = min(na, B + m.a[1][0]);
        if (m.a[1][1] < INF / 2) nb = min(nb, B + m.a[1][1]);
    }
    A = na;
    B = nb;
}

struct Pkg {
    bool empty = true;
    i128 first = 0, last = 0;
    i128 maxGap = 0;
    i128 minU = INF; // min(q_i - 2*q_{i+1})
    i128 minV = INF; // min(2*q_i - q_{i+1})
};

Pkg singlePkg(i128 x) {
    Pkg p;
    p.empty = false;
    p.first = p.last = x;
    return p;
}

Pkg joinPkg(const Pkg &a, const Pkg &b) {
    if (a.empty) return b;
    if (b.empty) return a;
    Pkg r;
    r.empty = false;
    r.first = a.first;
    r.last = b.last;
    r.maxGap = max({a.maxGap, b.maxGap, b.first - a.last});
    r.minU = min({a.minU, b.minU, a.last - 2 * b.first});
    r.minV = min({a.minV, b.minV, 2 * a.last - b.first});
    return r;
}

Pkg shiftPkg(Pkg p, i128 off) {
    if (p.empty) return p;
    p.first += off;
    p.last += off;
    if (p.minU < INF / 2) p.minU -= off;
    if (p.minV < INF / 2) p.minV += off;
    return p;
}

Pkg repeatPkg(const Pkg &p, int64 count, i128 off, int64 M) {
    Pkg result;
    if (p.empty || count == 0) return result;

    Pkg cur = p;
    int64 len = 1;
    int64 done = 0;
    int64 bits = count;

    while (bits > 0) {
        if (bits & 1) {
            result = joinPkg(result,
                             shiftPkg(cur, off + (i128)done * M));
            done += len;
        }
        bits >>= 1;
        if (!bits) break;
        cur = joinPkg(cur, shiftPkg(cur, (i128)len * M));
        len *= 2;
    }
    return result;
}

Mat gapMat(i128 a, i128 b, const Pkg &p) {
    if (p.empty) return zeroMat();

    i128 D = b - a;
    i128 X = b - p.first;
    i128 Y = p.last - a;

    i128 alpha = min(2 * X, Y);
    i128 beta = min(2 * X, 2 * Y);
    i128 gamma = min(X, Y);
    i128 delta = min(X, 2 * Y);

    if (p.first != p.last) {
        alpha = min(alpha, p.minU + 2 * b - a);
        beta = min(beta, 2 * D - 2 * p.maxGap);
        gamma = min(gamma, D - p.maxGap);
        delta = min(delta, p.minV + b - 2 * a);
    }

    Mat m;
    m.a[0][0] = alpha;
    m.a[0][1] = gamma;
    m.a[1][0] = beta;
    m.a[1][1] = delta;
    return m;
}

struct Node {
    bool hasCow = false;
    i128 firstCow = 0, lastCow = 0;
    Pkg pre, post;
    Mat R = identityMat();
};

Node combineNode(const Node &L, const Node &R) {
    Node res;
    res.hasCow = L.hasCow || R.hasCow;

    if (L.hasCow) res.firstCow = L.firstCow;
    else res.firstCow = R.firstCow;

    if (R.hasCow) res.lastCow = R.lastCow;
    else res.lastCow = L.lastCow;

    res.pre = L.hasCow ? L.pre : joinPkg(L.pre, R.pre);
    res.post = R.hasCow ? R.post : joinPkg(L.post, R.post);

    if (L.hasCow && R.hasCow) {
        Pkg between = joinPkg(L.post, R.pre);
        Mat middle = gapMat(L.lastCow, R.firstCow, between);
        res.R = multiply(multiply(L.R, middle), R.R);
    } else if (L.hasCow) {
        res.R = L.R;
    } else if (R.hasCow) {
        res.R = R.R;
    } else {
        res.R = identityMat();
    }
    return res;
}

struct APInterval {
    int type; // 0 cow, 1 package
    int64 startCol, endCol;
    int64 residue;
};

struct Event {
    int64 col;
    int idx;
    int type;
    int delta;
    bool operator<(const Event &other) const {
        return col < other.col;
    }
};

struct SegmentTree {
    int n;
    int64 M;
    vector<int64> coord;
    vector<int> cowCnt, pkgCnt;
    vector<Node> tree;

    SegmentTree(const vector<int64> &coords, int64 m)
        : n((int)coords.size()), M(m), coord(coords),
          cowCnt(coords.size(), 0), pkgCnt(coords.size(), 0),
          tree(4 * coords.size()) {
        build(1, 0, n - 1);
    }

    Node makeLeaf(int pos) {
        Node r;
        if (cowCnt[pos] > 0) {
            r.hasCow = true;
            r.firstCow = r.lastCow = (i128)coord[pos];
            r.R = cowCnt[pos] >= 2 ? zeroMat() : identityMat();
        } else {
            r.hasCow = false;
            Pkg p;
            if (pkgCnt[pos] > 0) p = singlePkg((i128)coord[pos]);
            r.pre = r.post = p;
            r.R = identityMat();
        }
        return r;
    }

    void build(int v, int l, int r) {
        if (l == r) {
            tree[v] = makeLeaf(l);
            return;
        }
        int mid = (l + r) / 2;
        build(v * 2, l, mid);
        build(v * 2 + 1, mid + 1, r);
        tree[v] = combineNode(tree[v * 2], tree[v * 2 + 1]);
    }

    void update(int pos, int type, int delta) {
        if (type == 0) cowCnt[pos] += delta;
        else pkgCnt[pos] += delta;
        update(1, 0, n - 1, pos);
    }

    void update(int v, int l, int r, int pos) {
        if (l == r) {
            tree[v] = makeLeaf(l);
            return;
        }
        int mid = (l + r) / 2;
        if (pos <= mid) update(v * 2, l, mid, pos);
        else update(v * 2 + 1, mid + 1, r, pos);
        tree[v] = combineNode(tree[v * 2], tree[v * 2 + 1]);
    }

    Node root() const {
        return tree[1];
    }
};

void printInt(i128 x) {
    if (x == 0) {
        cout << 0 << '\n';
        return;
    }
    string s;
    while (x > 0) {
        s.push_back(char('0' + x % 10));
        x /= 10;
    }
    reverse(s.begin(), s.end());
    cout << s << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int64 M;
    int N, P;
    cin >> M >> N >> P;

    vector<APInterval> intervals;
    vector<int64> residues;
    intervals.reserve(N + P);
    residues.reserve(N + P);

    auto readInterval = [&](int type) {
        int64 L, R;
        cin >> L >> R;
        int64 sc = L / M;
        int64 ec = R / M;
        int64 rem = L % M;
        intervals.push_back({type, sc, ec, rem});
        residues.push_back(rem);
    };

    for (int i = 0; i < N; i++) readInterval(0);
    for (int i = 0; i < P; i++) readInterval(1);

    sort(residues.begin(), residues.end());
    residues.erase(unique(residues.begin(), residues.end()), residues.end());

    vector<Event> events;
    events.reserve(2 * intervals.size());

    for (const auto &in : intervals) {
        int idx = lower_bound(residues.begin(), residues.end(), in.residue)
                  - residues.begin();
        events.push_back({in.startCol, idx, in.type, 1});
        events.push_back({in.endCol + 1, idx, in.type, -1});
    }

    sort(events.begin(), events.end());
    SegmentTree seg(residues, M);

    bool haveCow = false;
    i128 lastCow = 0;
    i128 A = 0, B = 0;
    Pkg pending;

    size_t i = 0;
    while (i < events.size()) {
        int64 col = events[i].col;
        size_t j = i;
        while (j < events.size() && events[j].col == col) {
            seg.update(events[j].idx, events[j].type, events[j].delta);
            j++;
        }

        if (j == events.size()) break;
        int64 nextCol = events[j].col;
        int64 width = nextCol - col;

        Node root = seg.root();
        i128 off = (i128)col * M;

        if (root.hasCow) {
            Pkg pre = shiftPkg(root.pre, off);
            i128 firstCowPos = off + root.firstCow;

            if (!haveCow) {
                Pkg all = joinPkg(pending, pre);
                i128 x = all.empty ? 0 : firstCowPos - all.first;
                A = 2 * x;
                B = x;
                haveCow = true;
            } else {
                Pkg all = joinPkg(pending, pre);
                Mat boundary = gapMat(lastCow, firstCowPos, all);
                applyMat(boundary, A, B);
            }

            Mat total = root.R;
            if (width > 1) {
                Pkg crossPkg = joinPkg(root.post, shiftPkg(root.pre, M));
                Mat cross = gapMat(root.lastCow, root.firstCow + M, crossPkg);
                Mat period = multiply(root.R, cross);
                total = multiply(powerMat(period, width - 1), root.R);
            }
            applyMat(total, A, B);

            i128 lastOff = ((i128)col + width - 1) * M;
            lastCow = lastOff + root.lastCow;
            pending = shiftPkg(root.post, lastOff);
        } else {
            if (!root.pre.empty) {
                Pkg all = repeatPkg(root.pre, width, off, M);
                pending = joinPkg(pending, all);
            }
        }

        i = j;
    }

    if (!haveCow) {
        cout << 0 << '\n';
        return 0;
    }

    i128 y = pending.empty ? 0 : pending.last - lastCow;
    i128 answer = min(A + y, B + 2 * y);
    printInt(answer);
    return 0;
}