题目 › usaco-1498

The Best Subsequence

USACO 2025 February · 金组 · 2025-02 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Farmer John has a binary string of length $N$ $(1 \leq N \leq 10^9)$, initially all zeros.

He will first perform $M$ ($1 \leq M \leq 2 \cdot 10^5$) updates on the string, in order. Each update flips every character from $l$ to $r$. Specifically, flipping a character changes it from $0$ to $1$, or vice versa.

Then, he asks you $Q$ ($1 \leq Q \leq 2 \cdot 10^5$) queries. For each query, he asks you to output the lexicographically greatest subsequence of length $k$ comprised of characters from the substring from $l$ to $r$. If your answer is a binary string $s_1s_2 \dots s_k$, then output $\sum_{i=0}^{k-1} 2^i \cdot s_{k-i}$ (that is, its value when interpreted as a binary number) modulo $10^9+7$.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Recall that string $A$ is lexicographically greater than string $B$ of equal length if and only if at the first position $i$, if it exists, where $A_i \neq B_i$, we have $A_i > B_i$.

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

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

The next $M$ lines contain two integers, $l$ and $r$ ($1 \leq l \leq r \leq N$) — the endpoints of each update.

The next $Q$ lines contain three integers, $l$, $r$, and $k$ ($1 \leq l \leq r \leq N, 1 \leq k \leq r - l + 1$) — the endpoints of each query and the length of the subsequence.

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

Output $Q$ lines. The $i$th line should contain the answer for the $i$th query.

SAMPLE INPUT:

5 3 9
1 5
2 4
3 3
1 5 5
1 5 4
1 5 3
1 5 2
1 5 1
2 5 4
2 5 3
2 5 2
2 5 1

SAMPLE OUTPUT:

21
13
7
3
1
5
5
3
1

After performing the $M$ operations, the string is $10101$.

For the first query, there is only one subsequence of length $5$, $10101$, which is interpreted as $1 \cdot 2^4 + 0 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 21$.

For the second query, there are $5$ unique subsequences of length $4$: $0101$, $1101$, $1001$, $1011$, $1010$. The lexicographically largest subsequence is $1101$, which is interpreted as $1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1\cdot 2^0 = 13$.

For the third query, the lexicographically largest sequence is $111$, which is interpreted as $7$.

SAMPLE INPUT:

9 1 1
7 9
1 8 8

SAMPLE OUTPUT:

3

SAMPLE INPUT:

30 1 1
1 30
1 30 30

SAMPLE OUTPUT:

73741816

Make sure to output the answer modulo $10^9+7$.

SCORING:

  • Input 4: $N \leq 10, Q \leq 1000$
  • Input 5: $M \leq 10$
  • Inputs 6-7: $N, Q \leq 1000$
  • Inputs 8-12: $N \leq 2 \cdot 10^5$
  • Inputs 13-20: No additional constraints.

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

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

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

代码

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

using ll = long long;
const ll MOD = 1000000007LL;

ll modPow(ll a, ll e) {
    ll res = 1;
    while (e > 0) {
        if (e & 1) res = res * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return res;
}

struct Run {
    ll start, len;
    int bit;
};

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

    ll N;
    int M, Q;
    cin >> N >> M >> Q;

    map<ll, int> diff;
    for (int i = 0; i < M; i++) {
        ll l, r;
        cin >> l >> r;
        diff[l] ^= 1;
        diff[r + 1] ^= 1;
    }

    vector<Run> runs;
    runs.reserve(diff.size() + 1);

    auto addRun = [&](ll start, ll len, int bit) {
        if (len <= 0) return;
        if (!runs.empty() && runs.back().bit == bit) {
            runs.back().len += len;
        } else {
            runs.push_back({start, len, bit});
        }
    };

    ll pos = 1;
    int curBit = 0;
    for (const auto &[x, delta] : diff) {
        if (x > pos) addRun(pos, x - pos, curBit);
        curBit ^= delta;
        pos = x;
    }
    if (pos <= N) addRun(pos, N - pos + 1, curBit);

    int R = (int)runs.size();
    vector<ll> starts(R);
    vector<ll> zeroPref(R + 1, 0);
    vector<ll> valuePref(R + 1, 0);

    for (int i = 0; i < R; i++) {
        starts[i] = runs[i].start;
        zeroPref[i + 1] = zeroPref[i] +
                          (runs[i].bit == 0 ? runs[i].len : 0);

        ll p = modPow(2, runs[i].len);
        ll add = runs[i].bit ? p - 1 : 0;
        valuePref[i + 1] = (valuePref[i] * p + add) % MOD;
    }

    auto runContaining = [&](ll x) {
        return int(upper_bound(starts.begin(), starts.end(), x) -
                   starts.begin() - 1);
    };

    auto zerosUpTo = [&](ll x) -> ll {
        if (x <= 0) return 0;
        int i = runContaining(x);
        ll result = zeroPref[i];
        if (runs[i].bit == 0) result += x - runs[i].start + 1;
        return result;
    };

    // Value of the binary string consisting of global positions 1..x.
    auto prefixValue = [&](ll x) -> ll {
        if (x <= 0) return 0;
        int i = runContaining(x);
        ll taken = x - runs[i].start + 1;
        if (taken == runs[i].len) return valuePref[i + 1];

        ll p = modPow(2, taken);
        ll result = valuePref[i] * p % MOD;
        if (runs[i].bit) result = (result + p - 1) % MOD;
        return result;
    };

    while (Q--) {
        ll l, r, k;
        cin >> l >> r >> k;

        ll length = r - l + 1;
        ll deletions = length - k;
        ll zerosBefore = zerosUpTo(l - 1);
        ll zeros = zerosUpTo(r) - zerosBefore;

        if (deletions >= zeros) {
            // There are enough ones to make the answer k consecutive ones.
            cout << (modPow(2, k) + MOD - 1) % MOD << '\n';
            continue;
        }

        // Delete the first `deletions` zeros in the query. If p is the
        // position immediately after them, the answer consists of all ones
        // before p, followed by the unchanged suffix s[p..r].
        ll p;
        if (deletions == 0) {
            p = l;
        } else {
            ll target = zerosBefore + deletions;
            int j = int(lower_bound(zeroPref.begin(), zeroPref.end(), target) -
                        zeroPref.begin());
            int i = j - 1;
            ll offset = target - zeroPref[i];
            ll zeroPosition = runs[i].start + offset - 1;
            p = zeroPosition + 1;
        }

        ll suffixLength = r - p + 1;
        ll A = prefixValue(p - 1);
        ll B = prefixValue(r);
        ll suffixPow = modPow(2, suffixLength);
        ll answerPow = modPow(2, k);

        ll product = (A + 1) % MOD * suffixPow % MOD;
        ll answer = (answerPow - product + MOD) % MOD;
        answer = (answer + B) % MOD;

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

    return 0;
}