Codeforces Round 1118 (Div. 2)
D
魔法方块
Magic Tiles
Codeforces
2500
原题
← CE →

魔法方块AI (CI翻译)

Magic Tiles is a well-known piano game worldwide, and this problem is based on it. Noyan is not very good at games that require fast reflexes and high concentration. As a result, he can only use a single finger, meaning he can press at most one tile per row. Furthermore, when he presses the same column consecutively, he earns points proportional to the length of his streak. However, if he presses an incorrect tile, he is eliminated. Help Noyan determine the optimal configuration to maximize his score.

《Magic Tiles》是一款风靡全球的钢琴游戏,本题以此为背景。Noyan 不太擅长需要快速反应和高度专注的游戏。因此,他只能用一根手指操作,也就是说,每行最多按下一个方块。此外,如果他连续按下同一列的方块,就能根据连续按下的长度获得相应的分数。但如果按错方块,他就会被淘汰。请帮助 Noyan 找到得分最高的方案。AI (CI翻译)

You are given a grid with $10^{18}$ rows and $2$ columns. Rows are indexed from $1$ to $10^{18}$, and columns are indexed $1$ and $2$. Each cell is colored either white or black.

给定一个有 $10^{18}$ 行、$2$ 列的网格。行的编号从 $1$ 到 $10^{18}$,列的编号为 $1$ 和 $2$。每个格子要么是白色,要么是黑色。AI (CI翻译)

You need to select a subset of black cells such that at most one cell is selected from each row. Your goal is to maximize the total score $S$, which is the sum of scores calculated for the first and second columns independently.

你需要选择一些黑色格子,并且每行最多选择一个格子。目标是最大化总分 $S$,其中 $S$ 是分别计算两列得分后相加的结果。AI (CI翻译)

The score for a single column is defined as $$ \sum_{i=1}^{k} 100^{100^{x_i}}, $$ where $k$ is the number of maximal contiguous segments of selected cells in that column, and $x_i$ represents the length of the $i$-th segment.

单列得分定义为 $$ \sum_{i=1}^{k} 100^{100^{x_i}}, $$ 其中 $k$ 是该列中被选格子构成的极大连续段数,$x_i$ 表示第 $i$ 段的长度。AI (CI翻译)

Determine the largest total score you can get.

求你能获得的最大总分。AI (CI翻译)

Since the grid is astronomically large, the input is given in a compressed format of maximal black segments for each column. You also need to provide the answer in a compressed format. For more details, refer to the input and output sections.

由于网格规模极大,输入会以每列黑色格子的极大连续段的压缩形式给出。你也需要以压缩形式输出答案。更多详情请参阅输入和输出部分。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 3000$). The description of the test cases follows.

每个输入包含多个测试用例。第一行包含测试用例数 $t$($1 \le t \le 3000$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of segments of black cells in Column 1 and Column 2, respectively.

每个测试用例的第一行包含两个整数 $n$ 和 $m$($1 \le n, m \le 3000$),分别表示第 1 列和第 2 列中黑色格子段的数量。AI (CI翻译)

The next $n$ lines of each test case contain the black cell segments for Column 1. The $i$-th of these lines contains two integers $l_{1, i}$ and $r_{1, i}$ ($1 \le l_{1, i} \le r_{1, i} \le 10^{18}$), representing a contiguous range of black cells from the $l_{1, i}$-th to the $r_{1, i}$-th row, inclusive.

接下来的 $n$ 行描述第 1 列的黑色格子段。其中第 $i$ 行包含两个整数 $l_{1, i}$ 和 $r_{1, i}$($1 \le l_{1, i} \le r_{1, i} \le 10^{18}$),表示从第 $l_{1, i}$ 行到第 $r_{1, i}$ 行(含端点)连续的一段黑色格子。AI (CI翻译)

The next $m$ lines of each test case contain the black cell segments for Column 2. The $j$-th of these lines contains two integers $l_{2, j}$ and $r_{2, j}$ ($1 \le l_{2, j} \le r_{2, j} \le 10^{18}$), representing a contiguous range of black cells from the $l_{2, j}$-th to the $r_{2, j}$-th row, inclusive.

接下来的 $m$ 行描述第 2 列的黑色格子段。其中第 $j$ 行包含两个整数 $l_{2, j}$ 和 $r_{2, j}$($1 \le l_{2, j} \le r_{2, j} \le 10^{18}$),表示从第 $l_{2, j}$ 行到第 $r_{2, j}$ 行(含端点)连续的一段黑色格子。AI (CI翻译)

Within the same column, no given segments touch one another. More formally, $r_{1, i} + 1 \lt l_{1, i+1}$ and $r_{2, j} + 1 \lt l_{2, j+1}$ for all valid $i$ and $j$.

同一列中,任意两个给定的黑色格子段都不会相邻。更形式化地说,对于所有合法的 $i$ 和 $j$,均有 $r_{1, i} + 1 \lt l_{1, i+1}$ 和 $r_{2, j} + 1 \lt l_{2, j+1}$。AI (CI翻译)

It's guaranteed that the sum of $n$ over all test cases doesn't exceed $3000$, and the sum of $m$ over all test cases doesn't exceed $3000$.

保证所有测试用例的 $n$ 之和不超过 $3000$,所有测试用例的 $m$ 之和不超过 $3000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output two lines.

对于每个测试用例,输出两行。AI (CI翻译)

The first line should contain a single integer $c$ ($1 \leq c$) — the total number of maximal contiguous segments selected across both columns.

第一行输出一个整数 $c$($1 \leq c$),表示两列中选出的极大连续段总数。AI (CI翻译)

The second line should contain $c$ integers $x_1, x_2, \ldots, x_c$ representing the lengths of all selected segments, sorted in non-increasing order ($x_1 \ge x_2 \ge \ldots \ge x_c \ge 1$).

第二行输出 $c$ 个整数 $x_1, x_2, \ldots, x_c$,表示所有选出段的长度,并按非递增顺序排序($x_1 \ge x_2 \ge \ldots \ge x_c \ge 1$)。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the two black segments do not overlap. We select rows $1$ through $3$ in the first column and rows $5$ through $6$ in the second column. This produces two segments with lengths $3$ and $2$.

在第一个测试用例中,两列的黑色格子段互不重叠。我们在第 1 列选择第 1 行至第 3 行,在第 2 列选择第 5 行至第 6 行。这样会得到长度分别为 $3$ 和 $2$ 的两段。AI (CI翻译)

In the second test case, both columns are black on rows $1$ through $4$. Since at most one cell may be selected from each row, we select all four cells from either one of the columns. This produces a single segment of length $4$.

在第二个测试用例中,两列的第 1 行至第 4 行都是黑色。由于每行最多只能选择一个格子,我们选择其中一列的全部四个格子。这样会得到一段长度为 $4$ 的连续段。AI (CI翻译)

In the third test case, one optimal selection is:

在第三个测试用例中,一种最优选择方案如下:AI (CI翻译)

  • rows $1$ and $2$ in the first column;
  • rows $3$ through $7$ in the second column;
  • row $8$ in the first column;
  • rows $9$ through $12$ in the second column.
  • 在第 1 列选择第 1 行和第 2 行;
  • 在第 2 列选择第 3 行至第 7 行;
  • 在第 1 列选择第 8 行;
  • 在第 2 列选择第 9 行至第 12 行。
AI (CI翻译)

Therefore, the selected segments in the first column have lengths $2$ and $1$, while the selected segments in the second column have lengths $5$ and $4$. After sorting all segment lengths in non-increasing order, the answer is $[5,4,2,1]$.

因此,第 1 列选出的连续段长度为 $2$ 和 $1$,第 2 列选出的连续段长度为 $5$ 和 $4$。将所有段长按非递增顺序排序后,答案为 $[5,4,2,1]$。AI (CI翻译)

It can be shown that all given constructions are optimal for their respective test cases.

可以证明,给出的所有方案对于各自的测试用例都是最优的。AI (CI翻译)

image image imageThe table of the first exampleThe table of the second exampleThe table of the third example In the diagrams above, red indicates selected cells.

image image image第一个样例的网格第二个样例的网格第三个样例的网格 在上面的图示中,红色表示被选中的格子。AI (CI翻译)

样例

Input #1
4
1 1
1 3
5 6
1 1
1 4
1 4
2 2
1 4
7 10
3 7
9 12
1 1
1 1000000000000000000
1 1
Output #1
2
3 2
1
4
4
5 4 2 1
1
1000000000000000000
InputCopy OutputCopy
题目信息
题号
2258D
时间限制
4 s
内存限制
512 MB
标签
动态规划
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。