题面
来源: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
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
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
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
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#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; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Clue { int r, c, p; }; struct FamilyData { bool hasO[2] = {false, false}; bool hasI[2] = {false, false}; int64 maxO[2] = {0, 0}; int64 minI[2] = {0, 0}; int64 maxI[2] = {0, 0}; vector<int64> valuesO[2]; }; static int parity(int64 x) { x %= 2; if (x < 0) x += 2; return static_cast<int>(x); } static int64 countWithParity(int64 lo, int64 hi, int p) { if (lo > hi) return 0; if (parity(lo) != p) ++lo; if (lo > hi) return 0; return (hi - lo) / 2 + 1; } static FamilyData buildData(const vector<Clue>& clues, bool useSum) { FamilyData d; for (const Clue& clue : clues) { int64 t = useSum ? int64(clue.r) + clue.c : int64(clue.c) - clue.r; int q = parity(t + clue.p); int o = q; if (!d.hasO[o]) { d.hasO[o] = true; d.maxO[o] = t; } else { d.maxO[o] = max(d.maxO[o], t); } d.valuesO[o].push_back(t); int in = 1 - q; if (!d.hasI[in]) { d.hasI[in] = true; d.minI[in] = d.maxI[in] = t; } else { d.minI[in] = min(d.minI[in], t); d.maxI[in] = max(d.maxI[in], t); } } for (int p = 0; p < 2; ++p) { sort(d.valuesO[p].begin(), d.valuesO[p].end()); } return d; } // One-transition grids: parameter k lies in [A, B-1]. static int64 countHalfplanes(int64 A, int64 B, const FamilyData& d) { int64 result = 0; for (int p = 0; p < 2; ++p) { int64 lo = A; int64 hi = B - 1; if (d.hasO[p]) lo = max(lo, d.maxO[p]); if (d.hasI[p]) hi = min(hi, d.minI[p] - 1); result += countWithParity(lo, hi, p); } return result; } // Two-transition grids: parameters l,u satisfy A <= l < u <= B, // u-l >= M, and l,u have the same parity. static int64 countBands(int64 A, int64 B, int64 M, const FamilyData& d) { int64 result = 0; int64 lastL = B - M; if (lastL < A) return 0; for (int p = 0; p < 2; ++p) { int64 hiL = lastL; if (d.hasI[p]) hiL = min(hiL, d.minI[p] - 1); int64 l = A; if (parity(l) != p) ++l; const vector<int64>& v = d.valuesO[p]; size_t ptr = 0; for (; l <= hiL; l += 2) { while (ptr < v.size() && v[ptr] <= l) ++ptr; int64 loU = l + M; if (d.hasI[p]) loU = max(loU, d.maxI[p] + 1); int64 hiU = B; if (ptr < v.size()) hiU = min(hiU, v[ptr]); result += countWithParity(loU, hiU, p); } } return result; } 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 (Clue& clue : clues) { char v; cin >> v >> clue.r >> clue.c; clue.p = (v == '-'); } int pureCount = 0; for (int p = 0; p < 2; ++p) { bool ok = true; for (const Clue& clue : clues) { if (parity(int64(p) + clue.r + clue.c) != clue.p) { ok = false; break; } } pureCount += ok; } FamilyData differenceData = buildData(clues, false); FamilyData sumData = buildData(clues, true); const int64 diffA = 1LL - R; const int64 diffB = int64(C) - 1; const int64 sumA = 2; const int64 sumB = int64(R) + C; const int64 M = max(R, C); int64 answer = pureCount; answer += countHalfplanes(diffA, diffB, differenceData); if (min(R, C) > 1) { answer += countHalfplanes(sumA, sumB, sumData); // In the 2x2 case these two bands describe the same all-plus grid. if (!(R == 2 && C == 2)) { answer += countBands(diffA, diffB, M, differenceData); } answer += countBands(sumA, sumB, M, sumData); } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Memory { int r, c; int value; int parity; array<int, 4> dist; }; static int64 solveOneDimensional(int n, const vector<Memory>& memories, bool byRow) { vector<int> fixed(n + 1, 0); for (const auto& m : memories) { int pos = byRow ? m.r : m.c; fixed[pos] = m.value; } // State: last value, and whether the unique possible "++" has been used. int64 dp[2][2] = {}; auto index = [](int x) { return x == 1 ? 1 : 0; }; for (int v : {-1, 1}) { if (fixed[1] == 0 || fixed[1] == v) { dp[index(v)][0] = 1; } } for (int pos = 2; pos <= n; ++pos) { int64 ndp[2][2] = {}; for (int last = 0; last < 2; ++last) { int lastValue = last ? 1 : -1; for (int used = 0; used < 2; ++used) { int64 ways = dp[last][used]; if (!ways) continue; for (int v : {-1, 1}) { if (fixed[pos] != 0 && fixed[pos] != v) continue; if (v != lastValue) { ndp[index(v)][used] += ways; } else if (v == 1 && used == 0) { ndp[1][1] += ways; } } } } memcpy(dp, ndp, sizeof(dp)); } int64 answer = 0; for (int last = 0; last < 2; ++last) { for (int used = 0; used < 2; ++used) { answer += dp[last][used]; } } return answer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int64 R, C; int X; cin >> R >> C >> X; vector<Memory> memories(X); for (int i = 0; i < X; ++i) { char ch; int r, c; cin >> ch >> r >> c; memories[i].r = r; memories[i].c = c; memories[i].value = (ch == '+' ? 1 : -1); memories[i].parity = ((r + c) % 2 == 0 ? 1 : -1); } if (R == 1 || C == 1) { int n = (int)max(R, C); bool byRow = (C == 1); cout << solveOneDimensional(n, memories, byRow) << '\n'; continue; } const int64 cells = R * C; // A[k] = number of cells at Manhattan distance < k from a corner. // Only areas at most cells/2 are needed for the canonical representation. vector<int64> area(1, 0); const int maxPossibleK = (int)(R + C - 1); for (int k = 1; k <= maxPossibleK; ++k) { int d = k - 1; int64 lo = max<int64>(0, (int64)d - (C - 1)); int64 hi = min<int64>(R - 1, d); int64 diagonal = (hi >= lo ? hi - lo + 1 : 0); int64 nextArea = area.back() + diagonal; if (2 * nextArea > cells) break; area.push_back(nextArea); } const int Kmax = (int)area.size() - 1; // Corner order: top-left, top-right, bottom-left, bottom-right. vector<vector<pair<int, int>>> ordered(4); for (int i = 0; i < X; ++i) { auto& m = memories[i]; int64 r = m.r, c = m.c; m.dist[0] = (int)((r - 1) + (c - 1)); m.dist[1] = (int)((r - 1) + (C - c)); m.dist[2] = (int)((R - r) + (c - 1)); m.dist[3] = (int)((R - r) + (C - c)); for (int corner = 0; corner < 4; ++corner) { if (m.dist[corner] < Kmax) { ordered[corner].push_back({m.dist[corner], i}); } } } for (int corner = 0; corner < 4; ++corner) { sort(ordered[corner].begin(), ordered[corner].end()); } // A triangle of size k at a corner requires a particular checkerboard phase. int factor[4]; factor[0] = 1; factor[1] = (C % 2 == 1 ? 1 : -1); factor[2] = (R % 2 == 1 ? 1 : -1); factor[3] = ((R + C) % 2 == 0 ? 1 : -1); int64 answer = 0; const int minDim = (int)min(R, C); const int doubleLimit = min(Kmax, minDim - 1); for (int base : {1, -1}) { int mismatch = 0; vector<int> weight(X); for (int i = 0; i < X; ++i) { int baseValue = base * memories[i].parity; if (baseValue != memories[i].value) { ++mismatch; weight[i] = -1; } else { weight[i] = 1; } } // weights[corner][k]: total adjustment caused by flipped remembered cells. vector<vector<int>> weights(4, vector<int>(Kmax + 1, 0)); for (int corner = 0; corner < 4; ++corner) { int ptr = 0; const auto& v = ordered[corner]; for (int k = 1; k <= Kmax; ++k) { int sum = weights[corner][k - 1]; while (ptr < (int)v.size() && v[ptr].first < k) { sum += weight[v[ptr].second]; ++ptr; } weights[corner][k] = sum; } } // Unmodified checkerboard. if (mismatch == 0) ++answer; // One flipped corner triangle. for (int corner = 0; corner < 4; ++corner) { for (int k = 1; k <= Kmax; ++k) { int sizeSign = (k % 2 == 0 ? 1 : -1); if (factor[corner] * sizeSign != base) continue; if (2 * area[k] == cells && base == -1) continue; if (mismatch + weights[corner][k] == 0) { ++answer; } } } // Two triangles at opposite corners. Their sizes must have disjoint supports. const pair<int, int> oppositePairs[2] = {{0, 3}, {1, 2}}; for (auto [first, second] : oppositePairs) { if (doubleLimit < 1) continue; int firstParity = (base * factor[first] == -1 ? 1 : 0); int secondParity = (base * factor[second] == -1 ? 1 : 0); unordered_map<int, vector<int>> positions; positions.reserve((size_t)min<int64>(doubleLimit + 1, 2LL * X + 16)); int secondStart = (secondParity == 1 ? 1 : 2); for (int l = secondStart; l <= doubleLimit; l += 2) { positions[weights[second][l]].push_back(l); } int firstStart = (firstParity == 1 ? 1 : 2); for (int k = firstStart; k <= doubleLimit; k += 2) { int maxL = min(doubleLimit, minDim - k); if (maxL < 1) continue; // Binary search the largest size satisfying the canonical area rule. int lo = 1, hi = maxL, best = 0; while (lo <= hi) { int mid = (lo + hi) / 2; int64 twiceArea = 2 * (area[k] + area[mid]); bool ok = (base == 1 ? twiceArea <= cells : twiceArea < cells); if (ok) { best = mid; lo = mid + 1; } else { hi = mid - 1; } } if (best == 0) continue; int wanted = -mismatch - weights[first][k]; auto it = positions.find(wanted); if (it == positions.end()) continue; const auto& vec = it->second; answer += (int64)(upper_bound(vec.begin(), vec.end(), best) - vec.begin()); } } } cout << answer << '\n'; } return 0; }
#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; }
#include <bits/stdc++.h> using namespace std; struct Clue { char v; int r, c; }; static long long countOdd(long long l, long long r) { if (l > r) return 0; if (l % 2 == 0) ++l; if (l > r) return 0; return (r - l) / 2 + 1; } static long long countParity(int l, int r, int parity) { if (l > r) return 0; int first = l; if ((first & 1) != parity) ++first; if (first > r) return 0; return (r - first) / 2 + 1; } 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) { cin >> cl.v >> cl.r >> cl.c; } if (R == 1 || C == 1) { const int N = (R == 1 ? C : R); // The two strictly alternating strings. bool alternatingOK[2] = {true, true}; // Candidates with their unique ++ defect starting at position j. // Process candidates separately according to the parity of j. int lower[2] = {1, 1}; int upper[2] = {N - 1, N - 1}; for (const auto &cl : clues) { const int i = (R == 1 ? cl.c : cl.r); const bool desiredPlus = (cl.v == '+'); const int minus = (cl.v == '-'); const bool plusInFirst = (i % 2 == 1); if ((alternatingOK[0] || alternatingOK[1])) { if ((plusInFirst != desiredPlus)) alternatingOK[0] = false; if ((!plusInFirst != desiredPlus)) alternatingOK[1] = false; } const int goodLeftParity = (i & 1) ^ minus; const int goodRightParity = ((i - 1) & 1) ^ minus; for (int parity = 0; parity < 2; ++parity) { if (parity != goodLeftParity) { upper[parity] = min(upper[parity], i - 1); } if (parity != goodRightParity) { lower[parity] = max(lower[parity], i); } } } long long answer = 0; answer += alternatingOK[0]; answer += alternatingOK[1]; for (int parity = 0; parity < 2; ++parity) { answer += countParity(lower[parity], upper[parity], parity); } cout << answer << '\n'; continue; } // Write a cell as a base checkerboard value flipped according to z. // A cell is '+' exactly when z == ((r+c)&1). // Every non-checkerboard solution, except all-plus on 2x2, belongs // to one of four threshold families in s=r+c or d=c-r. bool checkerOK[2] = {true, true}; bool allPlus = true; const long long M = (long long)R + C; const long long dMin = 1LL - C; const long long dMax = (long long)R - 1; // z = 1 iff s >= k. long long spL = 3, spR = M; // z = 1 iff s <= k. long long smL = 3, smR = M - 1; // z = 1 iff d >= k. long long dpL = dMin + 1, dpR = dMax; // z = 1 iff d <= k. long long dmL = dMin, dmR = dMax - 1; for (const auto &cl : clues) { const int parity = (cl.r + cl.c) & 1; const int minus = (cl.v == '-'); const int requiredZ = parity ^ minus; if (requiredZ != 0) checkerOK[0] = false; if (requiredZ != 1) checkerOK[1] = false; if (minus) allPlus = false; const long long s = (long long)cl.r + cl.c; const long long d = (long long)cl.c - cl.r; if (requiredZ == 1) spR = min(spR, s); else spL = max(spL, s + 1); if (requiredZ == 1) smL = max(smL, s); else smR = min(smR, s - 1); if (requiredZ == 1) dpR = min(dpR, d); else dpL = max(dpL, d + 1); if (requiredZ == 1) dmL = max(dmL, d); else dmR = min(dmR, d - 1); } long long answer = 0; answer += checkerOK[0]; answer += checkerOK[1]; answer += countOdd(spL, spR); answer += countOdd(smL, smR); answer += countOdd(dpL, dpR); answer += countOdd(dmL, dmR); if (R == 2 && C == 2 && allPlus) { ++answer; } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Clue { int r, c, val; }; struct Solver { int R, C, N; vector<Clue> clues; int cornerParity[4]; static int paritySign(int x) { return (x & 1) ? -1 : 1; } static long long countWithParity(int L, int U, int par) { if (L > U) return 0; int first = L; if ((first & 1) != par) ++first; if (first > U) return 0; return (U - first) / 2 + 1; } int distanceToCorner(int q, const Clue &cl) const { int r = cl.r, c = cl.c; if (q == 0) return r + c - 1; // top-left if (q == 1) return r + C - c; // top-right if (q == 2) return R + C - r - c + 1; // bottom-right return R - r + c; // bottom-left } long long countPlain() const { long long result = 0; for (int base : {-1, 1}) { bool ok = true; for (const Clue &cl : clues) { int expected = base * paritySign(cl.r + cl.c); if (expected != cl.val) { ok = false; break; } } result += ok; } return result; } long long countSingle(int q) const { int L[2] = {1, 1}; int U[2] = {N, N}; for (const Clue &cl : clues) { int d = distanceToCorner(q, cl); int s = cl.val * paritySign(cl.r + cl.c) * cornerParity[q]; int outsideParity = (s == 1 ? 0 : 1); for (int par = 0; par < 2; ++par) { if (par == outsideParity) { // The cell must be outside the triangle. U[par] = min(U[par], d - 1); } else { // The cell must be inside the triangle. L[par] = max(L[par], d); } } } long long result = 0; for (int par = 0; par < 2; ++par) result += countWithParity(L[par], U[par], par); return result; } long long countPair(int diagonal) const { const int q1 = diagonal; // 0: TL+BR, 1: TR+BL const int q2 = diagonal + 2; const int sizeParityChange = (R + C) & 1; long long result = 0; for (int par = 0; par < 2; ++par) { int maxK = N - 1; int maxL = N; vector<int> lowBucket(N + 3, 1); vector<int> upBucket(N + 3, N); for (const Clue &cl : clues) { int d1 = distanceToCorner(q1, cl); int d2 = distanceToCorner(q2, cl); int s = cl.val * paritySign(cl.r + cl.c) * cornerParity[q1]; int outsideParity = (s == 1 ? 0 : 1); if (par == outsideParity) { // The cell must be outside both triangles. maxK = min(maxK, d1 - 1); maxL = min(maxL, d2 - 1); } else { // The cell must be inside exactly one triangle. int K = min(d1, N + 1); int lowRequirement = min(d2, N + 1); int upRequirement = min(d2 - 1, N); lowBucket[K] = max(lowBucket[K], lowRequirement); upBucket[K] = min(upBucket[K], upRequirement); } } // futureLow[k] is the maximum lower bound among constraints // whose first-triangle threshold is greater than k. vector<int> futureLow(N + 3, 1); for (int k = N; k >= 1; --k) futureLow[k] = max(lowBucket[k + 1], futureLow[k + 1]); int secondParity = (par + sizeParityChange) & 1; int prefixUp = N; for (int k = 1; k <= maxK; ++k) { prefixUp = min(prefixUp, upBucket[k]); if ((k & 1) != par) continue; int low = max(1, futureLow[k]); int up = min(maxL, N - k); up = min(up, prefixUp); result += countWithParity(low, up, secondParity); } } return result; } bool matchesSingle(int q, int k) const { int base = cornerParity[q] * ((k & 1) ? -1 : 1); for (const Clue &cl : clues) { int expected = base * paritySign(cl.r + cl.c); if (k >= distanceToCorner(q, cl)) expected = -expected; if (expected != cl.val) return false; } return true; } bool matchesPair(int q, int k, int l) const { int base = cornerParity[q] * ((k & 1) ? -1 : 1); for (const Clue &cl : clues) { int expected = base * paritySign(cl.r + cl.c); bool inside = false; if (k >= distanceToCorner(q, cl)) inside = !inside; if (l >= distanceToCorner(q + 2, cl)) inside = !inside; if (inside) expected = -expected; if (expected != cl.val) return false; } return true; } long long solve2D() { cornerParity[0] = paritySign(1 + 1); cornerParity[1] = paritySign(1 + C); cornerParity[2] = paritySign(R + C); cornerParity[3] = paritySign(R + 1); long long answer = countPlain(); for (int q = 0; q < 4; ++q) answer += countSingle(q); answer += countPair(0); answer += countPair(1); // Remove representations of the same grid with opposite baseline. if (R == C) { for (int q = 0; q < 2; ++q) { if (matchesSingle(q, N)) --answer; if (matchesSingle(q, N - 1)) --answer; } } else if (abs(R - C) == 1) { for (int q = 0; q < 2; ++q) { if (matchesSingle(q, N)) --answer; } } // The all-plus 2 by 2 grid has one further duplicate representation. if (R == 2 && C == 2 && matchesPair(0, 1, 1)) --answer; return answer; } }; long long solveOneDimensional(int length, const vector<pair<int, int>> &clues) { long long answer = 0; // Perfectly alternating strings. for (int start : {-1, 1}) { bool ok = true; for (auto [pos, val] : clues) { int expected = start * Solver::paritySign(pos - 1); if (expected != val) { ok = false; break; } } answer += ok; } if (length == 1) return answer; // Strings whose only ++ pair starts at position d. int L[2] = {1, 1}; int U[2] = {length - 1, length - 1}; for (auto [pos, val] : clues) { int p = Solver::paritySign(pos); int requiredParity = (val * p == 1 ? 0 : 1); for (int par = 0; par < 2; ++par) { if (par == requiredParity) L[par] = max(L[par], pos); // d >= pos else U[par] = min(U[par], pos - 1); // d < pos } } for (int par = 0; par < 2; ++par) answer += Solver::countWithParity(L[par], U[par], par); 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 (Clue &cl : clues) { char ch; cin >> ch >> cl.r >> cl.c; cl.val = (ch == '+' ? 1 : -1); } if (R == 1 || C == 1) { int length = max(R, C); vector<pair<int, int>> oneDimensionalClues; oneDimensionalClues.reserve(X); for (const Clue &cl : clues) { int pos = (R == 1 ? cl.c : cl.r); oneDimensionalClues.push_back({pos, cl.val}); } cout << solveOneDimensional(length, oneDimensionalClues) << '\n'; } else { Solver solver; solver.R = R; solver.C = C; solver.N = min(R, C); solver.clues = std::move(clues); cout << solver.solve2D() << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Clue { int r, c, val; // val: +1 for '+', -1 for '-' }; // 1D automaton states: // 0: start A // 1: B = {0,1} // 2: C = {-1,0} // 3: D = {0,1,2} // 4: E = {-1,0,1} int transState[5][2] = { {1, 2}, // A: '+' -> B, '-' -> C {3, 2}, // B {1, -1}, // C {-1, 4}, // D {3, -1} // E }; long long countParity(int L, int U, int p) { if (L > U) return 0; auto parity = [](int x) { int m = x % 2; if (m < 0) m += 2; return m; }; while (parity(L) != p) ++L; while (parity(U) != p) --U; if (L > U) return 0; return (U - L) / 2 + 1; } long long solve1D(int R, int C, const vector<Clue>& clues) { int N = (R == 1 ? C : R); vector<int> clue(N + 1, 0); // 0 unknown, 1 plus, -1 minus for (auto &cl : clues) { int pos = (R == 1 ? cl.c : cl.r); clue[pos] = cl.val; } long long dp[5] = {0}; dp[0] = 1; for (int pos = 1; pos <= N; ++pos) { long long ndp[5] = {0}; int mask = 3; if (clue[pos] == 1) mask = 1; else if (clue[pos] == -1) mask = 2; for (int st = 0; st < 5; ++st) { if (!dp[st]) continue; if (mask & 1) { // plus int ns = transState[st][0]; if (ns != -1) ndp[ns] += dp[st]; } if (mask & 2) { // minus int ns = transState[st][1]; if (ns != -1) ndp[ns] += dp[st]; } } for (int i = 0; i < 5; ++i) dp[i] = ndp[i]; } long long ans = 0; for (int st = 1; st < 5; ++st) ans += dp[st]; return ans; } long long count2xC(int C, const vector<int>& top, const vector<int>& bot) { long long dp[25] = {0}; dp[0] = 1; // state (start, start) for (int pos = 1; pos <= C; ++pos) { long long ndp[25] = {0}; int maskTop = 3; if (top[pos] == 1) maskTop = 1; else if (top[pos] == -1) maskTop = 2; int maskBot = 3; if (bot[pos] == 1) maskBot = 1; else if (bot[pos] == -1) maskBot = 2; for (int s1 = 0; s1 < 5; ++s1) { for (int s2 = 0; s2 < 5; ++s2) { long long cur = dp[s1 * 5 + s2]; if (!cur) continue; for (int c1 = 0; c1 < 2; ++c1) { if (!(maskTop & (1 << c1))) continue; int ns1 = transState[s1][c1]; if (ns1 == -1) continue; for (int c2 = 0; c2 < 2; ++c2) { if (!(maskBot & (1 << c2))) continue; // A column cannot be "--" if (c1 == 1 && c2 == 1) continue; int ns2 = transState[s2][c2]; if (ns2 == -1) continue; ndp[ns1 * 5 + ns2] += cur; } } } } for (int i = 0; i < 25; ++i) dp[i] = ndp[i]; } long long ans = 0; for (int i = 0; i < 25; ++i) ans += dp[i]; return ans; } long long countCutType(int R, int C, const vector<Clue>& clues, bool isSum) { int L0, U0; if (isSum) { L0 = 2; U0 = R + C - 1; } else { L0 = 1 - C; U0 = R - 2; } int low[2] = {L0, L0}; int high[2] = {U0, U0}; for (auto &cl : clues) { int s = isSum ? (cl.r + cl.c) : (cl.r - cl.c); int B = ((cl.r + cl.c) & 1) ? -1 : 1; bool targetPlus = (cl.val * B == 1); for (int p = 0; p < 2; ++p) { bool evenK = (p == 0); bool needLe = (evenK == targetPlus); if (needLe) { // Need s <= K, i.e. K >= s low[p] = max(low[p], s); } else { // Need s > K, i.e. K <= s - 1 high[p] = min(high[p], s - 1); } } } long long ans = 0; for (int p = 0; p < 2; ++p) { int L = max(L0, low[p]); int U = min(U0, high[p]); ans += countParity(L, U, p); } return ans; } long long solveTemplates(int R, int C, const vector<Clue>& clues) { long long ans = 0; bool okPlus = true; bool okMinus = true; for (auto &cl : clues) { int B = ((cl.r + cl.c) & 1) ? -1 : 1; if (cl.val != B) okPlus = false; if (cl.val != -B) okMinus = false; } ans += (okPlus ? 1 : 0); ans += (okMinus ? 1 : 0); ans += countCutType(R, C, clues, true); ans += countCutType(R, C, clues, false); return ans; } 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; clues.reserve(X); for (int i = 0; i < X; ++i) { string v; int r, c; cin >> v >> r >> c; int val = (v[0] == '+' ? 1 : -1); clues.push_back({r, c, val}); } long long ans; if (R == 1 || C == 1) { ans = solve1D(R, C, clues); } else if (R == 2 || C == 2) { if (R == 2) { vector<int> top(C + 1, 0), bot(C + 1, 0); for (auto &cl : clues) { if (cl.r == 1) top[cl.c] = cl.val; else bot[cl.c] = cl.val; } ans = count2xC(C, top, bot); } else { // Transpose: original C == 2, R > 2 int newC = R; vector<int> top(newC + 1, 0), bot(newC + 1, 0); for (auto &cl : clues) { if (cl.c == 1) top[cl.r] = cl.val; else bot[cl.r] = cl.val; } ans = count2xC(newC, top, bot); } } else { ans = solveTemplates(R, C, clues); } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; static inline int mod2(ll x) { int m = int(x % 2); if (m < 0) m += 2; return m; } ll countParity(ll low, ll high, int p) { if (low > high) return 0; ll first = low; if (mod2(first) != p) ++first; if (first > high) return 0; return (high - first) / 2 + 1; } ll countB(ll a, ll bL, ll bR, ll L, ll R, ll M, int p) { if (a < L || a > R) return 0; if (a > L && mod2(a) != p) return 0; ll low = max(bL, a); ll upperSmall = min(bR, R - 1); if (a > L) { low = max(low, a + M - 1); } ll res = 0; if (low <= upperSmall) { res += countParity(low, upperSmall, p); } // b == R has no parity / length restriction if (bL <= R && R <= bR && a <= R) { ++res; } return res; } // mark[i] : 1 = must be inside interval, -1 = must be outside, 0 = free ll countIntervals(ll L, ll R, ll M, int p, const vector<int>& mark) { int N = int(R - L + 1); bool hasInc = false; ll minInc = LLONG_MAX, maxInc = LLONG_MIN; for (int i = 0; i < N; ++i) { if (mark[i] == 1) { hasInc = true; ll c = L + i; minInc = min(minInc, c); maxInc = max(maxInc, c); } } if (!hasInc) { ll total = 1; // empty interval int i = 0; while (i < N) { while (i < N && mark[i] == -1) ++i; if (i >= N) break; int start = i; while (i < N && mark[i] != -1) ++i; int finish = i - 1; ll gapEnd = L + finish; for (int idx = start; idx <= finish; ++idx) { ll a = L + idx; total += countB(a, a, gapEnd, L, R, M, p); } } return total; } // If any forbidden coordinate lies between minInc and maxInc, impossible. for (int i = 0; i < N; ++i) { if (mark[i] == -1) { ll c = L + i; if (minInc <= c && c <= maxInc) return 0; } } ll aL = L, aR = minInc; ll bL = maxInc, bR = R; for (int i = 0; i < N; ++i) { if (mark[i] == -1) { ll c = L + i; if (c < minInc) { aL = max(aL, c + 1); } else if (c > maxInc) { bR = min(bR, c - 1); } } } if (aL > aR || bL > bR) return 0; ll total = 0; for (ll a = aL; a <= aR; ++a) { total += countB(a, bL, bR, L, R, M, p); } return total; } 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<tuple<int, int, int>> cons; cons.reserve(X); for (int i = 0; i < X; ++i) { char ch; int r, c; cin >> ch >> r >> c; int v = (ch == '+') ? 1 : -1; cons.emplace_back(r, c, v); } // One-dimensional case if (R == 1 || C == 1) { int N = max(R, C); vector<int> fixed(N + 1, -1); // 1 = '+', 0 = '-' for (auto [r, c, v] : cons) { int pos = (R == 1) ? c : r; fixed[pos] = (v == 1) ? 1 : 0; } ll dp[2][2] = {}; // last: 0 = '-', 1 = '+' // used: number of "++" substrings, 0 or 1 for (int pos = 1; pos <= N; ++pos) { ll ndp[2][2] = {}; for (int val = 0; val <= 1; ++val) { if (fixed[pos] != -1 && fixed[pos] != val) continue; if (pos == 1) { ndp[val][0] += 1; } else { for (int last = 0; last < 2; ++last) { for (int used = 0; used < 2; ++used) { if (dp[last][used] == 0) continue; int nused = used; if (val == 0) { if (last == 0) continue; // "--" } else { if (last == 1) { ++nused; if (nused > 1) continue; } } ndp[val][nused] += dp[last][used]; } } } } memcpy(dp, ndp, sizeof(dp)); } ll ans = 0; for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) ans += dp[a][b]; cout << ans << '\n'; continue; } // Tiny 2x2 brute force if (R == 2 && C == 2) { int fixed[2][2] = {}; for (auto [r, c, v] : cons) { fixed[r - 1][c - 1] = v; } ll ans = 0; for (int mask = 0; mask < 16; ++mask) { int g[2][2]; bool ok = true; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { int bit = i * 2 + j; int val = ((mask >> bit) & 1) ? 1 : -1; g[i][j] = val; if (fixed[i][j] != 0 && fixed[i][j] != val) ok = false; } } if (!ok) continue; auto goodPair = [](int a, int b) { int s = a + b; return -1 <= s && s <= 2; }; if (!goodPair(g[0][0], g[0][1])) continue; if (!goodPair(g[1][0], g[1][1])) continue; if (!goodPair(g[0][0], g[1][0])) continue; if (!goodPair(g[0][1], g[1][1])) continue; ++ans; } cout << ans << '\n'; continue; } // General 2D case int L1 = 2; int R1 = R + C; int N1 = R1 - L1 + 1; vector<int> mark1(N1, 0); int L2 = 1 - C; int R2 = R - 1; int N2 = R2 - L2 + 1; vector<int> mark2(N2, 0); bool conflict1 = false; bool conflict2 = false; bool all0 = true; bool all1 = true; for (auto [r, c, v] : cons) { int b = ((r + c) % 2 == 0) ? 1 : -1; int xval = (v == b) ? 0 : 1; if (xval != 0) all0 = false; if (xval != 1) all1 = false; // Type 1: x = 1 on an interval of r+c int s = r + c; int idx1 = s - L1; int want1 = (xval == 1) ? 1 : -1; if (mark1[idx1] == 0) mark1[idx1] = want1; else if (mark1[idx1] != want1) conflict1 = true; // Type 2: x = 0 on an interval of r-c int vc = r - c; int idx2 = vc - L2; int want2 = (xval == 0) ? 1 : -1; if (mark2[idx2] == 0) mark2[idx2] = want2; else if (mark2[idx2] != want2) conflict2 = true; } ll M = max(R, C) - 1; ll type1 = 0; ll type2 = 0; if (!conflict1) { type1 = countIntervals(L1, R1, M, 1, mark1); } if (!conflict2) { type2 = countIntervals(L2, R2, M, 0, mark2); } ll inter = (all0 ? 1 : 0) + (all1 ? 1 : 0); ll ans = type1 + type2 - inter; cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Clue { int r, c, v; }; long long count1D(int n, int L1, int L2, const vector<pair<int,int>>& cons) { vector<int> val(n + 2, 0); for (auto [idx, v] : cons) { if (idx < 1 || idx > n) return 0; if (val[idx] != 0 && val[idx] != v) return 0; val[idx] = v; } int M = max(L1, L2); int Smin; if (M <= 1) Smin = 2; else Smin = (M % 2 ? M : M - 1); // No defects: alternating sequence. long long noDef = 0; int req = 0; bool ok = true; for (int i = 1; i <= n; i++) { if (val[i] == 0) continue; int base = (i & 1) ? -1 : 1; // (-1)^i int need = val[i] * base; if (req == 0) req = need; else if (req != need) { ok = false; break; } } if (ok) noDef = (req == 0 ? 2 : 1); const int INF = n + 1; vector<array<int, 2>> prefMax(n + 2), prefMin(n + 2); vector<array<int, 2>> suffMax(n + 2), suffMin(n + 2); prefMax[0] = {0, 0}; prefMin[0] = {INF, INF}; for (int i = 1; i <= n; i++) { prefMax[i] = prefMax[i - 1]; prefMin[i] = prefMin[i - 1]; if (val[i] != 0) { int vBit = (val[i] == -1 ? 1 : 0); int p0 = vBit ^ (i & 1); prefMax[i][p0] = max(prefMax[i][p0], i); prefMin[i][p0] = min(prefMin[i][p0], i); } } suffMax[n + 1] = {0, 0}; suffMin[n + 1] = {INF, INF}; for (int i = n; i >= 1; i--) { suffMax[i] = suffMax[i + 1]; suffMin[i] = suffMin[i + 1]; if (val[i] != 0) { int vBit = (val[i] == -1 ? 1 : 0); int p0 = vBit ^ (i & 1); suffMax[i][p0] = max(suffMax[i][p0], i); suffMin[i][p0] = min(suffMin[i][p0], i); } } long long single = 0; long long two = 0; for (int e = 1; e <= n - 1; e++) { int p = e & 1; // For the single-defect sequence with defect at e. int L = max(prefMax[e][p], suffMax[e + 1][p ^ 1]); int R = min(suffMin[e + 1][p], prefMin[e][p ^ 1]); if (R == INF) single++; // Add a second defect e2 > e. int q = p ^ 1; int lo = max({e + 1, L, e + Smin}); int hi = min(n - 1, R - 1); if (lo <= hi) { int first = lo; if ((first & 1) != q) first++; if (first <= hi) { two += (hi - first) / 2 + 1; } } } return noDef + single + two; } 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; clues.reserve(X); vector<pair<int, int>> consS, consD; consS.reserve(X); consD.reserve(X); for (int i = 0; i < X; i++) { char ch; int r, c; cin >> ch >> r >> c; int v = (ch == '+' ? 1 : -1); clues.push_back({r, c, v}); consS.push_back({r + c - 1, v}); consD.push_back({r - c + C, v}); } int n = R + C - 1; long long ans; if (min(R, C) == 1) { ans = count1D(n, C, R, consS); } else { long long cntS = count1D(n, C, R, consS); long long cntD = count1D(n, C, R, consD); long long inter = 0; // The two checkerboard grids. for (int sgn : {1, -1}) { bool good = true; for (auto &cl : clues) { int parity = (cl.r + cl.c) & 1; int expected = (parity == 0 ? 1 : -1) * sgn; if (expected != cl.v) { good = false; break; } } if (good) inter++; } // The all-plus grid is also both sum- and difference-constant // only in the 2x2 case. if (R == 2 && C == 2) { bool good = true; for (auto &cl : clues) { if (cl.v != 1) { good = false; break; } } if (good) inter++; } ans = cntS + cntD - inter; } cout << ans << '\n'; } return 0; }
// YOUR CODE HERE
#include <bits/stdc++.h> using namespace std; struct Cell { int r, c; bool plus; }; /* For each parameter value, records whether it is invalid for at least one constraint. Parameter parities are stored separately. */ struct ParityBad { int n; vector<int> diff[2]; explicit ParityBad(int n = 0) : n(n), diff[2](vector<int>(n + 3, 0)) {} void add(int l, int r, int parity) { l = max(l, 1); r = min(r, n); if (l > r) return; if ((l & 1) != parity) ++l; if ((r & 1) != parity) --r; if (l > r) return; ++diff[parity][l]; --diff[parity][r + 2]; } vector<int> build() const { vector<int> bad(n + 1, 0); int count[2] = {0, 0}; for (int i = 1; i <= n; ++i) { count[i & 1] += diff[i & 1][i]; bad[i] = count[0] + count[1]; } return bad; } }; static int parityOf(int x) { return (x % 2 + 2) % 2; } /* For a defective row H_h, the cell in column c is '+' exactly when h and c have the same parity and h >= c, or h and c have different parity and h < c. This routine marks parameter values for which h = coef * parameter + constant gives the wrong remembered sign. */ static void markAffineWrong( ParityBad &bad, int coef, int constant, int c, bool wantedPlus, int L, int R ) { if (L > R) return; int constantParity = parityOf(constant); for (int p = 0; p < 2; ++p) { int hParity = (p + constantParity) & 1; bool sameParity = (hParity == (c & 1)); // Actual sign is '+' iff: // same parity: h >= c // other parity: h < c bool wrongMeansAtLeastC; if (wantedPlus) { wrongMeansAtLeastC = !sameParity; } else { wrongMeansAtLeastC = sameParity; } if (wrongMeansAtLeastC) { // h >= c if (coef == 1) { int low = c - constant; bad.add(max(L, low), R, p); } else { int high = constant - c; bad.add(L, min(R, high), p); } } else { // h < c if (coef == 1) { int high = c - constant - 1; bad.add(L, min(R, high), p); } else { int low = constant - c + 1; bad.add(max(L, low), R, p); } } } } static bool hCellIsPlus(int h, int c) { if ((h & 1) == (c & 1)) return h >= c; return h < c; } /* baseType: 0 -> + - + - ... 1 -> - + - + ... */ static bool patternPlus(int baseType, int c, int distanceParity) { bool base = (baseType == 0) ? ((c & 1) != 0) : ((c & 1) == 0); return base ^ (distanceParity != 0); } static long long countOneD(int length, const vector<pair<int, bool>> &cells) { long long answer = 0; bool alternatingPlusStart = true; bool alternatingMinusStart = true; for (auto [c, plus] : cells) { if (plus != ((c & 1) != 0)) alternatingPlusStart = false; if (plus != ((c & 1) == 0)) alternatingMinusStart = false; } answer += alternatingPlusStart; answer += alternatingMinusStart; int n = length - 1; if (n == 0) return answer; ParityBad bad(n); for (auto [c, plus] : cells) { // Here h = parameter. markAffineWrong(bad, 1, 0, c, plus, 1, n); } vector<int> badValues = bad.build(); for (int h = 1; h <= n; ++h) { if (badValues[h] == 0) ++answer; } return answer; } static long long countTwoByTwo(const vector<Cell> &cells) { long long answer = 0; for (int mask = 0; mask < 16; ++mask) { bool valid = true; auto plusAt = [&](int r, int c) { int bit = (r - 1) * 2 + (c - 1); return ((mask >> bit) & 1) != 0; }; for (int r = 1; r <= 2 && valid; ++r) { for (int c = 1; c <= 2; ++c) { if (c == 1 && !plusAt(r, c) && !plusAt(r, c + 1)) valid = false; if (r == 1 && !plusAt(r, c) && !plusAt(r + 1, c)) valid = false; } } for (const Cell &cell : cells) { if (plusAt(cell.r, cell.c) != cell.plus) { valid = false; break; } } answer += valid; } return answer; } class GridCounter { int R, C, n; vector<Cell> cells; int topBaseType(bool endsAtN) const { if (!endsAtN) return 0; return (C & 1) ? 0 : 1; } int bottomBaseType(bool startsAtN) const { if (!startsAtN) return 0; return (C & 1) ? 0 : 1; } long long countCheckerboard(int phase) const { for (const Cell &cell : cells) { bool expected = (((cell.r + cell.c) & 1) == 0); if (phase) expected = !expected; if (expected != cell.plus) return 0; } return 1; } // A defective block touching the top boundary, followed by pure rows. long long countTopSingle(bool endsAtN) const { int K = min(n, R - 1); if (K <= 0) return 0; ParityBad bad(K); for (const Cell &cell : cells) { int r = cell.r; if (endsAtN) { // h = n - a + r markAffineWrong(bad, -1, n + r, cell.c, cell.plus, r, K); } else { // h = a - r + 1 markAffineWrong(bad, 1, 1 - r, cell.c, cell.plus, r, K); } // If a < r, this cell belongs to the pure suffix. int pureRight = min(K, r - 1); if (pureRight >= 1) { int baseType = topBaseType(endsAtN); for (int p = 0; p < 2; ++p) { int distanceParity = (r & 1) ^ p ^ 1; bool expected = patternPlus(baseType, cell.c, distanceParity); if (expected != cell.plus) { bad.add(1, pureRight, p); } } } } vector<int> badValues = bad.build(); long long answer = 0; for (int a = 1; a <= K; ++a) { answer += (badValues[a] == 0); } return answer; } // A defective block touching the bottom boundary, preceded by pure rows. long long countBottomSingle(bool startsAtN) const { int K = min(n, R - 1); if (K <= 0) return 0; ParityBad bad(K); for (const Cell &cell : cells) { int r = cell.r; int firstBottomRow = R - r + 1; if (startsAtN) { // h = n - b - r + R + 1 markAffineWrong( bad, -1, n - r + R + 1, cell.c, cell.plus, firstBottomRow, K ); } else { // h = b + r - R markAffineWrong( bad, 1, r - R, cell.c, cell.plus, firstBottomRow, K ); } // If b < R-r+1, this cell is in the pure prefix. int pureRight = min(K, firstBottomRow - 1); if (pureRight >= 1) { int baseType = bottomBaseType(startsAtN); for (int p = 0; p < 2; ++p) { int distanceParity = (R & 1) ^ p ^ (r & 1); bool expected = patternPlus(baseType, cell.c, distanceParity); if (expected != cell.plus) { bad.add(1, pureRight, p); } } } } vector<int> badValues = bad.build(); long long answer = 0; for (int b = 1; b <= K; ++b) { answer += (badValues[b] == 0); } return answer; } /* Two boundary blocks: top ends at H_1 and bottom starts at H_n, or top ends at H_n and bottom starts at H_1. */ long long countTwoBoundaryBlocks(bool topEndsAtN) const { bool bottomStartsAtN = !topEndsAtN; ParityBad topBad(n), bottomBad(n); for (const Cell &cell : cells) { int r = cell.r; if (topEndsAtN) { markAffineWrong(topBad, -1, n + r, cell.c, cell.plus, r, n); } else { markAffineWrong(topBad, 1, 1 - r, cell.c, cell.plus, r, n); } int firstBottomRow = R - r + 1; if (bottomStartsAtN) { markAffineWrong( bottomBad, -1, n - r + R + 1, cell.c, cell.plus, firstBottomRow, n ); } else { markBadBottomStart: markAffineWrong( bottomBad, 1, r - R, cell.c, cell.plus, firstBottomRow, n ); } } vector<int> topBadValues = topBad.build(); vector<int> bottomBadValues = bottomBad.build(); // Prefix counts of valid bottom lengths, separated by parity. vector<array<int, 2>> pref(n + 1); pref[0] = {0, 0}; for (int b = 1; b <= n; ++b) { pref[b] = pref[b - 1]; if (bottomBadValues[b] == 0) ++pref[b][b & 1]; } /* For each parity of a, find the first remembered row in the pure middle whose sign would be wrong there. */ const int INF = R + 1; vector<vector<int>> firstWrong(2, vector<int>(R + 2, INF)); for (const Cell &cell : cells) { int baseType = topBaseType(topEndsAtN); for (int p = 0; p < 2; ++p) { int distanceParity = (cell.r & 1) ^ p ^ 1; bool expected = patternPlus(baseType, cell.c, distanceParity); if (expected != cell.plus) { firstWrong[p][cell.r] = cell.r; } } } for (int p = 0; p < 2; ++p) { for (int r = R; r >= 1; --r) { firstWrong[p][r] = min(firstWrong[p][r], firstWrong[p][r + 1]); } } int maxSum = min(R - 1, C); long long answer = 0; for (int a = 1; a <= n; ++a) { if (topBadValues[a] != 0) continue; int high = min(n, maxSum - a); if (high < 1) continue; int low = 1; int wrongRow = firstWrong[a & 1][a + 1]; if (wrongRow != INF) { low = max(low, R - wrongRow + 1); } if (low > high) continue; // R-a-b must have the same parity as C. int requiredParity = (R & 1) ^ (C & 1) ^ (a & 1); answer += pref[high][requiredParity] - pref[low - 1][requiredParity]; } return answer; } // An interior defective block spanning H_1, ..., H_n. long long countInterior(bool increasing) const { int maxL = R - n; if (maxL < 2) return 0; ParityBad bad(maxL); for (const Cell &cell : cells) { int r = cell.r; // Cell lies in the defective block. int blockLeft = max(2, r - n + 1); int blockRight = min(maxL, r); if (increasing) { // h = -l + r + 1 markAffineWrong( bad, -1, r + 1, cell.c, cell.plus, blockLeft, blockRight ); } else { // h = l + n - r markAffineWrong( bad, 1, n - r, cell.c, cell.plus, blockLeft, blockRight ); } // Cell lies in the pure prefix. int leftLeft = r + 1; int leftRight = maxL; if (leftLeft <= leftRight) { int baseType = increasing ? 0 : ((C & 1) ? 0 : 1); for (int p = 0; p < 2; ++p) { int distanceParity = p ^ 1 ^ (r & 1); bool expected = patternPlus(baseType, cell.c, distanceParity); if (expected != cell.plus) { bad.add(leftLeft, leftRight, p); } } } // Cell lies in the pure suffix. int rightLeft = 2; int rightRight = min(maxL, r - n); if (rightLeft <= rightRight) { int baseType = increasing ? ((C & 1) ? 0 : 1) : 0; for (int p = 0; p < 2; ++p) { int distanceParity = (r & 1) ^ p ^ (n & 1); bool expected = patternPlus(baseType, cell.c, distanceParity); if (expected != cell.plus) { bad.add(rightLeft, rightRight, p); } } } } vector<int> badValues = bad.build(); long long answer = 0; for (int l = 2; l <= maxL; ++l) { answer += (badValues[l] == 0); } return answer; } // All rows are defective, with a monotone sequence of defect positions. long long countAllDefectiveMonotone(bool increasing) const { int low, high; if (increasing) { low = 1; high = n - R + 1; } else { low = R; high = n; } if (low > high) return 0; ParityBad bad(high - low + 1); for (const Cell &cell : cells) { if (increasing) { // h = a + r - 1 markAffineWrong( bad, 1, cell.r - 1, cell.c, cell.plus, low, high ); } else { // h = a - r + 1 markAffineWrong( bad, 1, 1 - cell.r, cell.c, cell.plus, low, high ); } } vector<int> badValues = bad.build(); long long answer = 0; for (int a = low; a <= high; ++a) { answer += (badValues[a - low] == 0); } return answer; } /* All rows are defective and the special jump H_1 <-> H_n is used. C must be even. */ long long countSpecialAllDefective(bool jumpOneToN) const { int low, high; if (jumpOneToN) { low = max(1, R - n); high = min(n, R - 1); } else { low = max(1, n + 2 - R); high = min(n, 2 * n + 1 - R); } if (low > high) return 0; ParityBad bad(high - low + 1); for (const Cell &cell : cells) { int r = cell.r; if (jumpOneToN) { // Before the jump: h = k-r+1. markAffineWrong( bad, 1, 1 - r, cell.c, cell.plus, max(low, r), high ); // The row containing H_n. int k = r - 1; if (k >= low && k <= high && !hCellIsPlus(n, cell.c)) { bad.add(k - low, k - low, k & 1); } // After the jump: h = n+k+1-r. markAffineWrong( bad, 1, n + 1 - r, cell.c, cell.plus, low, min(high, r - 2) ); } else { // Before the jump: h = a+r-1. markAffineWrong( bad, 1, r - 1, cell.c, cell.plus, low, min(high, n - r + 1) ); // The row containing H_1. int a = n - r; if (a >= low && a <= high && !hCellIsPlus(1, cell.c)) { bad.add(a - low, a - low, a & 1); } // After the jump: h = a+r-n-1. markAffineWrong( bad, 1, r - n - 1, cell.c, cell.plus, low, min(high, n - r - 1) ); } } vector<int> badValues = bad.build(); long long answer = 0; for (int p = low; p <= high; ++p) { answer += (badValues[p - low] == 0); } return answer; } public: GridCounter(int R, int C, vector<Cell> cells) : R(R), C(C), n(C - 1), cells(std::move(cells)) {} long long solve() const { long long answer = 0; answer += countCheckerboard(0); answer += countCheckerboard(1); answer += countTopSingle(false); answer += countTopSingle(true); answer += countBottomSingle(false); answer += countBottomSingle(true); answer += countTwoBoundaryBlocks(false); answer += countTwoBoundaryBlocks(true); answer += countInterior(true); answer += countInterior(false); answer += countAllDefectiveMonotone(true); answer += countAllDefectiveMonotone(false); if ((C & 1) == 0) { answer += countSpecialAllDefective(true); answer += countSpecialAllDefective(false); } 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<Cell> cells; cells.reserve(X); for (int i = 0; i < X; ++i) { char value; int r, c; cin >> value >> r >> c; cells.push_back({r, c, value == '+'}); } if (R == 1 || C == 1) { int length = max(R, C); vector<pair<int, bool>> lineCells; lineCells.reserve(X); for (const Cell &cell : cells) { int position = (R == 1) ? cell.c : cell.r; lineCells.push_back({position, cell.plus}); } cout << countOneD(length, lineCells) << '\n'; continue; } if (R == 2 && C == 2) { cout << countTwoByTwo(cells) << '\n'; continue; } // The cases C=2 have a simpler row description. Transpose them so // that the main algorithm can assume C>=3. if (C == 2) { swap(R, C); for (Cell &cell : cells) swap(cell.r, cell.c); } GridCounter solver(R, C, std::move(cells)); cout << solver.solve() << '\n'; } return 0; }
#include <bits/stdc++.h> #include <boost/multiprecision/cpp_int.hpp> using namespace std; using boost::multiprecision::cpp_int; /* A row of length C is valid exactly when: - it contains no "--"; - it contains at most one "++". There are C+1 possible valid rows. Number them from 0 through C: - state 0 is the alternating row beginning with '+'; - state C is the alternating row beginning with '-'; - state p, 1 <= p < C, is the alternating row whose only equal adjacent pair is "++" between positions p and p+1. For two consecutive rows, their equality positions must all contain '+'. Moreover, each column may contain at most one such vertical "++". Consequently, walking through the row states uses labeled edges, with each label used at most once. The compatibility graph of row states is a theta graph: - most edges have one label; - one edge has no label and may be used repeatedly; - at most one additional edge has two labels. The memoized search below stores the used labels as a union of intervals. */ struct Edge { int to; vector<int> labels; }; struct Interval { int l, r; bool operator<(const Interval& other) const { return tie(l, r) < tie(other.l, other.r); } }; static bool contains(const vector<Interval>& used, int x) { for (const auto& in : used) { if (in.l <= x && x <= in.r) return true; } return false; } static vector<Interval> addLabels(const vector<Interval>& used, const vector<int>& labels) { vector<Interval> result = used; for (int x : labels) { bool inserted = false; for (auto& in : result) { if (in.l <= x && x <= in.r) { inserted = true; break; } } if (inserted) continue; result.push_back({x, x}); sort(result.begin(), result.end()); vector<Interval> merged; for (const auto& in : result) { if (!merged.empty() && in.l <= merged.back().r + 1) { merged.back().r = max(merged.back().r, in.r); } else { merged.push_back(in); } } result.swap(merged); } return result; } static bool cellIsPlus(int state, int col, int C) { if (state == 0) { return (col & 1) != 0; } if (state == C) { return (col & 1) == 0; } if (col <= state) { return ((col + state) & 1) == 0; } else { return ((col + state) & 1) != 0; } } static string makeKey(int row, int state, const vector<Interval>& used) { string key; key.reserve(24 + 16 * used.size()); auto appendInt = [&](int x) { for (int i = 0; i < 4; ++i) { key.push_back(static_cast<char>((x >> (8 * i)) & 255)); } }; appendInt(row); appendInt(state); appendInt(static_cast<int>(used.size())); for (const auto& in : used) { appendInt(in.l); appendInt(in.r); } return key; } 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<tuple<char, int, int>> clues(X); for (auto& [ch, r, c] : clues) { cin >> ch >> r >> c; --r; --c; } // Use the smaller dimension as the number of rows. if (R > C) { swap(R, C); for (auto& [ch, r, c] : clues) { swap(r, c); } } vector<vector<pair<int, int>>> rowClues(R); for (const auto& [ch, r, c] : clues) { rowClues[r].push_back({c, ch == '+'}); } vector<vector<Edge>> graph(C + 1); auto addEdge = [&](int u, int v, vector<int> labels) { graph[u].push_back({v, labels}); graph[v].push_back({u, move(labels)}); }; if (C == 1) { addEdge(0, 1, {}); addEdge(0, 0, {1}); } else if (C == 2) { addEdge(0, 1, {1}); addEdge(1, 2, {2}); addEdge(0, 2, {}); addEdge(1, 1, {1, 2}); } else if (C & 1) { for (int i = 0; i + 1 < C; ++i) { addEdge(i, i + 1, {i + 1}); } addEdge(0, C - 1, {C}); addEdge(0, C, {}); } else { for (int i = 0; i + 1 < C; ++i) { addEdge(i, i + 1, {i + 1}); } addEdge(C - 1, C, {C}); addEdge(0, C, {}); addEdge(1, C - 1, {1, C}); } unordered_map<string, cpp_int> memo; memo.reserve(1 << 18); memo.max_load_factor(0.7f); auto dfs = [&](auto&& self, int row, int state, const vector<Interval>& used) -> cpp_int { for (const auto& [col, wantPlus] : rowClues[row]) { if (cellIsPlus(state, col, C) != wantPlus) { return 0; } } if (row + 1 == R) { return 1; } string key = makeKey(row, state, used); auto it = memo.find(key); if (it != memo.end()) return it->second; cpp_int answer = 0; for (const Edge& e : graph[state]) { bool usable = true; for (int label : e.labels) { if (contains(used, label)) { usable = false; break; } } if (!usable) continue; vector<Interval> nextUsed = addLabels(used, e.labels); answer += self(self, row + 1, e.to, nextUsed); } memo.emplace(move(key), answer); return answer; }; vector<Interval> empty; cpp_int answer = dfs(dfs, 0, 0, empty); cout << answer << '\n'; } return 0; }