题目 › usaco-1549

Lineup Counting Queries

USACO 2026 First · 铂金组 · 2026-01 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

There is a line of cows, initially (i.e. at time $t = 0$) containing only cow $0$ at position $0$ (here, a cow is at position $k$ if there are $k$ cows in front of it). At time $t$ for $t=1,2,3,\dots$, the cow at position 0 moves to position $\lfloor t/2\rfloor$, every cow in positions $1\dots \lfloor t/2\rfloor$ moves forward one position, and cow $t$ joins the line at the end of the line (position $t$).

Answer $Q$ ($1\le Q\le 10^5$) independent queries each of the following form:

  • Out of cows $l_1\dots r_1$, how many are located at positions $l_2\dots r_2$ immediately after time $t$? ($0\le l_1\le r_1\le t, 0\le l_2\le r_2 \le t, t\le 10^{18}$)

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

The first line contains $Q$, the number of queries.

The next $Q$ lines each contain five integers specifying a query of the form "$l_1$ $r_1$ $l_2$ $r_2$ $t$."

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

Output the answer to each query on a separate line.

SAMPLE INPUT:

4
0 9 0 9 9
3 5 4 5 9
4 5 3 5 9
1 1 3 3 9

SAMPLE OUTPUT:

10
2
1
1

Lineups at various times:

t = 0 | 0
t = 1 | 0 1
t = 2 | 1 0 2
t = 3 | 0 1 2 3
t = 4 | 1 2 0 3 4
t = 5 | 2 0 1 3 4 5
t = 6 | 0 1 3 2 4 5 6
t = 7 | 1 3 2 0 4 5 6 7
t = 8 | 3 2 0 4 1 5 6 7 8
t = 9 | 2 0 4 1 3 5 6 7 8 9

At $t=9$ the cows from front to back are $[2,0,4,1,3,5,6,7,8,9]$.

To answer the third query, the cows at positions $3\dots 5$ are $[1,3,5]$, and only one of them is in the range $4\dots 5$.

SAMPLE INPUT:

1
0 1000000000000000000 0 1000000000000000000 1000000000000000000

SAMPLE OUTPUT:

1000000000000000001

SCORING:

  • Input 3: $Q\le 1000, t\le 100$
  • Inputs 4-7: $l_1 = r_1$ for all queries
  • Inputs 8-14: $r_1 \leq 2 \cdot l_1$ for all queries
  • Inputs 15-21: No additional constraints

Problem credits: Agastya Goel and Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 55.0k平均输出 token(含思考) ↓
  • 127.3输出速度 tokens/s ↑
  • 397 秒延迟 ↓
  • 0.41×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 28.6采样 1 · 答案错误
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 51.0k平均输出 token(含思考) ↓
  • 119.9输出速度 tokens/s ↑
  • 450 秒延迟 ↓
  • 0.20×CPU 时间 / 官方解 ↓
  • 更优 2时间复杂度(相对官方解)

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

代码

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

using int64 = long long;

// f(r) is the next time at which a cow returns to position 0,
// given that its previous return was at time r.
static inline int64 next_reset(int64 r) {
    return (3 * (r + 1)) / 2;
}

// Minimum r such that f(r) >= y.
static inline int64 inverse_threshold(int64 y) {
    if (y <= 0) return 0;
    return (2 * y + 2) / 3 - 1;
}

// Minimum k >= 1 such that 3*k - 1 >= x.
static inline int64 min_cow(int64 x) {
    return (x + 3) / 3;
}

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

    int Q;
    cin >> Q;

    while (Q--) {
        int64 l1, r1, l2, r2, t;
        cin >> l1 >> r1 >> l2 >> r2 >> t;

        if (t == 0) {
            cout << ((l1 == 0 && l2 == 0) ? 1 : 0) << '\n';
            continue;
        }

        int64 answer = 0;

        // Cows which have never entered the moving prefix remain at
        // position equal to their cow number.
        int64 active_max = (t + 1) / 2;
        int64 lo = max({l1, active_max + 1, l2});
        int64 hi = min({r1, t, r2});
        if (lo <= hi) answer += hi - lo + 1;

        // Cow 0 has the same reset-time process, starting at time 0.
        int64 reset = 0;
        while (reset < t) reset = next_reset(reset);
        int64 pos0 = reset - t;
        if (l1 == 0 && l2 <= pos0 && pos0 <= r2) ++answer;

        if (r1 >= 1 && l1 <= active_max) {
            // For cow k >= 1, write x = 3*k - 1. Its reset times are
            // x, f(x), f^2(x), ... . At time t, if f^i(x) is the first
            // reset time >= t, its position is f^i(x) - t.
            //
            // b, c, d are inverse thresholds:
            // b: minimum initial x with f^i(x) >= t
            // c: minimum initial x with f^i(x) >= t+l2
            // d: minimum initial x with f^i(x) >= t+r2+1
            int64 b = t;
            int64 c = t + l2;
            int64 d = t + r2 + 1;
            int64 previous_b = 0;

            for (int i = 0;; ++i) {
                int64 lower = max({int64(1), l1, min_cow(b), min_cow(c)});
                int64 upper = min({active_max, r1, d / 3});

                // For i >= 1, this group also requires
                // f^(i-1)(x) < t, equivalently x < previous_b.
                if (i > 0) upper = min(upper, previous_b / 3);

                if (lower <= upper) answer += upper - lower + 1;

                // Once min_cow(b) == 1, this is the final group and all
                // remaining active cows have been covered.
                if (min_cow(b) <= 1) break;

                previous_b = b;
                b = inverse_threshold(b);
                c = inverse_threshold(c);
                d = inverse_threshold(d);
            }
        }

        cout << answer << '\n';
    }

    return 0;
}