题目 › usaco-1550

Pluses and Minuses

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

查看原题跳到结果

题面

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

Farmer John once painted a rectangular grid on the ground of his pasture. In each cell, he painted either a $+$ or a $−$ (representing $+1$ and $−1$, respectively).

Over time, the paint faded, and Farmer John now remembers the values of only some cells. However, Farmer John does remember one important fact about the original painting:

In every row and every column, the sum of the values in any contiguous subsegment was always between $−1$ and $2$ (inclusive).

As an example, consider the row $\texttt{+ - - +}$. It does not satisfy the condition, since the subsegment $\texttt{+ [ - - ] +}$ has sum $-2$.

However, the row $\texttt{- + + -}$ does satisfy the condition.

[ - ] + + -    sum = -1
[ - + ] + -    sum = 0
[ - + + ] -    sum = +1
[ - + + - ]    sum = 0
- [ + ] + -    sum = +1
- [ + + ] -    sum = +2
- [ + + - ]    sum = +1
- + [ + ] -    sum = +1
- + [ + - ]    sum = 0
- + + [ - ]    sum = -1

Count the number of different grids consistent with Farmer John's memory.

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

The first line contains $T$ ($1\le T\le 100$), the number of independent tests. Each test is specified as follows:

The first line contains $R$, $C$, and $X$ ($1\le R,C\le 5\cdot 10^5$, $0\le X\le \min(10^5,RC)$), meaning that the grid has dimensions $R\times C$ and Farmer John remembers the values of $X$ different cells in the grid.

Then following $X$ lines each contain a character $v\in \{+, -\}$ followed by two integers $r$ and $c$ ($1\le r\le R, 1\le c\le C$), meaning that the value at the $r$th row and $c$th column of the grid is $v$. It is guaranteed that no ordered pair $(r,c)$ appears more than once within a single test.

Additionally, it is guaranteed that neither the sum of $R$ nor the sum of $C$ over all tests exceeds $10^6$, and that the sum of $X$ over all tests does not exceed $2\cdot 10^5$.

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

For each test, output the number of grids on a separate line.

SAMPLE INPUT:

2
1 3 3
+ 1 3
+ 1 1
- 1 2
1 3 3
+ 1 1
+ 1 3
+ 1 2

SAMPLE OUTPUT:

1
0

SAMPLE INPUT:

1
2 2 0

SAMPLE OUTPUT:

7

Here are the seven grids:

++
++

++
+-

++
-+

+-
++

+-
-+

-+
++

-+
+-

SCORING:

  • Inputs 3-4: $\min(R,C)=1$ for all tests
  • Inputs 5-6: $R,C\le 10$ for all tests
  • Inputs 7-11: $\sum \max(R,C)^2 \le 10^6$
  • Inputs 12-14: $\sum RC \le 10^6$
  • Inputs 15-22: No additional constraints.

Problem credits: Alex Chen

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 75.3k平均输出 token(含思考) ↓
  • 22.5输出速度 tokens/s ↑
  • 3179 秒延迟 ↓
  • 1.31×CPU 时间 / 官方解 ↓
  • 不确定 2、更差 1时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 18.2采样 2 · 答案错误
  • 18.2采样 3 · 答案错误
  • 34.5k平均输出 token(含思考) ↓
  • 23.5输出速度 tokens/s ↑
  • 1645 秒延迟 ↓
  • 0.80×CPU 时间 / 官方解 ↓
  • 不确定 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

0/3

解出的采样 ↑

  • 0.0采样 1 · 编译错误
  • 0.0采样 2 · 编译错误
  • 0.0采样 3 · 编译错误
  • 122.5k平均输出 token(含思考) ↓
  • 92.4输出速度 tokens/s ↑
  • 1436 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • —时间复杂度(相对官方解)

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

代码

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

using int64 = long long;

struct Clue {
    int r, c, x; // x = 0 for '+', 1 for '-'
};

static int parity(int64 x) {
    int p = int(x % 2);
    return p < 0 ? p + 2 : p;
}

static int64 floorDiv2(int64 x) {
    if (x >= 0) return x / 2;
    return -((-x + 1) / 2);
}

static int64 ceilDiv2(int64 x) {
    return floorDiv2(x + 1);
}

// Number of integers in [lo, hi] with the requested parity.
static int64 countWithParity(int64 lo, int64 hi, int p) {
    if (lo > hi) return 0;
    int64 first = lo + (p - parity(lo) + 2) % 2;
    if (first > hi) return 0;
    return (hi - first) / 2 + 1;
}

// Count L in [l0,l1], H in [h0,h1], both of parity p,
// subject to H-L >= separation.
static int64 countPairs(int64 l0, int64 l1,
                        int64 h0, int64 h1,
                        int p, int64 separation) {
    if (l0 > l1 || h0 > h1) return 0;

    int64 il = ceilDiv2(l0 - p);
    int64 ir = floorDiv2(l1 - p);
    int64 jl = ceilDiv2(h0 - p);
    int64 jr = floorDiv2(h1 - p);
    if (il > ir || jl > jr) return 0;

    // L = p+2i, H = p+2j. We need j-i >= need.
    int64 need = (separation + 1) / 2;
    int64 result = 0;

    // i+need <= jl: every permissible j works.
    int64 fullEnd = min(ir, jl - need);
    if (il <= fullEnd) {
        int64 n = fullEnd - il + 1;
        result += n * (jr - jl + 1);
    }

    // jl < i+need <= jr: the number of j values decreases linearly.
    int64 st = max(il, jl - need + 1);
    int64 en = min(ir, jr - need);
    if (st <= en) {
        int64 n = en - st + 1;
        int64 A = jr - need + 1;
        result += A * n - (st + en) * n / 2;
    }

    return result;
}

static int checkerboardCount(const vector<Clue>& clues) {
    int answer = 0;
    for (int base = 0; base < 2; ++base) {
        bool ok = true;
        for (const auto& cl : clues) {
            int expected = base ^ parity(cl.r + cl.c);
            if (expected != cl.x) {
                ok = false;
                break;
            }
        }
        answer += ok;
    }
    return answer;
}

// orient = 0: q = r+c; orient = 1: q = r-c.
static int64 singleWallCount(int R, int C, int orient,
                             const vector<Clue>& clues) {
    int64 qmin = orient == 0 ? 2 : 1 - (int64)C;
    int64 qmax = orient == 0 ? R + (int64)C : R - 1;

    int64 answer = 0;
    for (int b = 0; b < 2; ++b) {
        int base = 1 - b;
        int64 lo = qmin, hi = qmax - 1;

        for (const auto& cl : clues) {
            int64 q = orient == 0 ? cl.r + (int64)cl.c
                                  : cl.r - (int64)cl.c;
            int requiredZ = cl.x ^ parity(q) ^ base;
            if (requiredZ) {
                lo = max(lo, q);       // q <= K
            } else {
                hi = min(hi, q - 1);   // q > K
            }
        }
        answer += countWithParity(lo, hi, b);
    }
    return answer;
}

static int64 doubleWallCount(int R, int C, int orient,
                             const vector<Clue>& clues) {
    int64 qmin = orient == 0 ? 2 : 1 - (int64)C;
    int64 qmax = orient == 0 ? R + (int64)C : R - 1;
    int64 separation = max(R, C);

    int64 answer = 0;

    for (int b = 0; b < 2; ++b) {
        int base = 1 - b;
        vector<int64> ones;
        ones.reserve(clues.size());

        bool hasZero = false;
        int64 minZero = 0, maxZero = 0;

        for (const auto& cl : clues) {
            int64 q = orient == 0 ? cl.r + (int64)cl.c
                                  : cl.r - (int64)cl.c;
            int requiredZ = cl.x ^ parity(q) ^ base;
            if (requiredZ) {
                ones.push_back(q);
            } else {
                if (!hasZero) {
                    hasZero = true;
                    minZero = maxZero = q;
                } else {
                    minZero = min(minZero, q);
                    maxZero = max(maxZero, q);
                }
            }
        }

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

        if (hasZero) {
            // All zeroes lie strictly between L and H. No one can lie in
            // their bounding interval; lower and upper ones force bounds.
            int64 l0 = qmin, l1 = minZero - 1;
            int64 h0 = maxZero + 1, h1 = qmax;

            auto it = lower_bound(ones.begin(), ones.end(), minZero);
            if (it != ones.begin()) {
                l0 = max(l0, *prev(it));
            }

            bool bad = (it != ones.end() && *it <= maxZero);
            if (bad) continue;

            auto jt = upper_bound(ones.begin(), ones.end(), maxZero);
            if (jt != ones.end()) {
                h1 = min(h1, *jt);
            }

            answer += countPairs(l0, l1, h0, h1, b, separation);
        } else {
            // The open interval (L,H) must contain no coordinate of a one.
            // Thus it lies in one of the gaps between consecutive such
            // coordinates. Different gaps count disjoint sets of pairs.
            ones.erase(unique(ones.begin(), ones.end()), ones.end());
            int k = int(ones.size());

            for (int t = 0; t <= k; ++t) {
                int64 l0 = qmin, l1 = qmax;
                int64 h0 = qmin, h1 = qmax;

                if (t > 0) {
                    int64 a = ones[t - 1];
                    l0 = max(l0, a);
                    h0 = max(h0, a + 1);
                }
                if (t < k) {
                    int64 bnd = ones[t];
                    l1 = min(l1, bnd - 1);
                    h1 = min(h1, bnd);
                }

                answer += countPairs(l0, l1, h0, h1, b, separation);
            }
        }
    }

    return answer;
}

static bool validLine(const vector<int>& a) {
    int plusRuns = 0;
    for (int i = 0; i < int(a.size()); ++i) {
        if (i > 0 && a[i - 1] == 1 && a[i] == 1) return false;
        if (i > 0 && a[i - 1] == 0 && a[i] == 0) ++plusRuns;
    }
    return plusRuns <= 1;
}

static int solveTwoByTwo(const vector<Clue>& clues) {
    int answer = 0;
    for (int mask = 0; mask < 16; ++mask) {
        vector<int> x(4);
        for (int i = 0; i < 4; ++i) x[i] = (mask >> i) & 1;

        bool ok = true;
        for (const auto& cl : clues) {
            int idx = (cl.r - 1) * 2 + (cl.c - 1);
            if (x[idx] != cl.x) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;

        ok &= validLine({x[0], x[1]});
        ok &= validLine({x[2], x[3]});
        ok &= validLine({x[0], x[2]});
        ok &= validLine({x[1], x[3]});
        answer += ok;
    }
    return answer;
}

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

    int T;
    cin >> T;
    while (T--) {
        int R, C, X;
        cin >> R >> C >> X;

        vector<Clue> clues(X);
        for (auto& cl : clues) {
            char ch;
            cin >> ch >> cl.r >> cl.c;
            cl.x = (ch == '-');
        }

        if (R == 2 && C == 2) {
            cout << solveTwoByTwo(clues) << '\n';
            continue;
        }

        int64 answer = checkerboardCount(clues);

        if (min(R, C) == 1) {
            // The two diagonal orientations coincide on a one-dimensional
            // grid, so only one is counted.
            answer += singleWallCount(R, C, 0, clues);
        } else {
            for (int orient = 0; orient < 2; ++orient) {
                answer += singleWallCount(R, C, orient, clues);
                answer += doubleWallCount(R, C, orient, clues);
            }
        }

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

    return 0;
}