#P15348. [TOIP 2025] 同色樓梯和雙色樓梯

[TOIP 2025] 同色樓梯和雙色樓梯

Problem Description

In an m×nm \times n grid, each cell has exactly one color, and we use uppercase English letters to represent each color.

A “stair” means: several consecutive vertical segments, where the bottom positions of these segments (the columns they are in) must be the same, and the height increases by 11 step by step from left to right. Note that the height and width of a stair shape must be equal.

A “monochrome stair” means: all cells covered by the stair have the same color.

A “two-color stair” means: the cells covered by the stair contain exactly two colors.

In the example figure below, the blue, yellow, and green shapes are “monochrome stairs” with width (height) 22, 33, and 44, respectively. Note that stairs may overlap. For example, next to the blue CC stair there is another CC stair. Also, any large stair must contain smaller stairs. For example, in the figure below, the width 33 BB stair contains 33 stairs of width 22. The width 44 AA stair contains 33 stairs of width 33 and 66 stairs of width 22.

:::align{center} :::

In the following example figure, all “two-color stairs” are marked.

:::align{center} :::

Your task is to compute, for each possible width (height), how many monochrome stairs or two-color stairs there are.

Input Format

$$\begin{aligned} &m \; n \\ &a_{1,1} a_{1,2} a_{1,3} \cdots a_{1,n} \\ &a_{2,1} a_{2,2} a_{2,3} \cdots a_{2,n} \\ &a_{3,1} a_{3,2} a_{3,3} \cdots a_{3,n} \\ &\vdots \\ &a_{m,1} a_{m,2} a_{m,3} \cdots a_{m,n} \\ &q \end{aligned}$$
  • mm and nn represent the height and width of the region, respectively.
  • ai,ja_{i,j} represents the color of the region.
  • If q=1q=1, query the number of monochrome stairs. If q=2q=2, query the number of two-color stairs.

Output Format

$$\begin{aligned} &k \\ &s_1 \; s_2 \; s_3 \; \cdots \; s_k \end{aligned}$$
  • kk is a non-negative integer, representing the maximum width (height) among all monochrome stairs (two-color stairs).
  • Each sis_i is a non-negative integer, representing the number of monochrome stairs (two-color stairs) with width (height) ii.
  • If k=0k = 0, output an empty line on the second line.
6 8
BCCDDAAA
CCCBBBAB
DDDAAAAB
EBBEAAAA
EBBAAAAA
BBBBABCD
1
4
48 12 4 1
3 3
BCC
CCC
DDD
1
2
9 2
3 3
BCC
CCC
DDD
2
3
0 2 1

Hint

Constraints

  • 1≤m≤40001 \le m \le 4000.
  • 1≤n≤40001 \le n \le 4000.
  • ai,ja_{i,j} are uppercase English letters.
  • mm and nn are integers.
  • q∈{1,2}q \in \{1,2\}.

Scoring

This problem has five subtasks, with the constraints as follows. Each subtask may contain one or more testdata, and you will receive the score for that subtask only if all testdata in the subtask are answered correctly.

Subtask Score Additional Input Constraints
1 5 q=1q=1, all ai,ja_{i,j} are the same.
2 9 q=1q=1, m≤200m \le 200, n≤200n \le 200.
3 22 q=1q=1, m≤200m \le 200.
4 33 q=1q=1.
5 31 q=2q=2.

Translated by ChatGPT 5