311396: CF1980F2. Field Division (hard version)

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F2. Field Division (hard version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.

Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).

Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:

  • To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field.
  • Alice's path will divide the field into two parts — one part will belong to Alice (this part includes the cells of her path), the other part — to Bob.
  • Alice will own the part that includes the cell ($n, 1$).
  • Bob will own the part that includes the cell ($1, m$).

Alice wants to divide the field in such a way as to get as many cells as possible.

Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ — the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot).

Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

Input

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) — the field sizes and the number of fountains, respectively.

Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) — the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).

It is guaranteed that the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, first output $\alpha$ — the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

ExampleInput
5
2 2 3
1 1
1 2
2 2
5 5 4
1 2
2 2
3 4
4 3
2 5 9
1 2
1 5
1 1
2 2
2 4
2 5
1 4
2 3
1 3
6 4 4
6 2
1 3
1 4
1 2
3 4 5
2 1
3 2
1 4
1 3
2 4
Output
1
1 0 1 
11
0 1 0 4 
1
0 0 1 1 0 0 0 0 0 
6
15 0 0 0 
1
2 3 0 0 0 
Note

Below are the images for the second example:

The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue.

Note that if Bob gives Alice fountain $1$ or fountain $3$, then that fountain cannot be on Alice's plot.

Output

题目大意:
这是一个较难版本的问题,与简单版本的区别仅在于问题的要求。简单版本只需要你打印某些值是否非零,而困难版本需要你打印确切的值。

爱丽丝和鲍勃正在划分一块田地。田地是一个大小为 $ n \times m $ ($ 2 \le n, m \le 10^9 $) 的矩形;行从上到下编号为 1 到 $ n $,列从左到右编号为 1 到 $ m $。行 $ r $ 和列 $ c $ 交叉的单元格表示为 ($ r, c $)。

鲍勃有 $ k $ ($ 2 \le k \le 2 \cdot 10^5 $) 个喷泉,它们都位于田地中的不同单元格。爱丽丝负责划分田地,但她必须满足几个条件:
1. 划分田地时,爱丽丝将从田地左侧或顶部的任意一个空闲(没有喷泉)单元格开始,每次移动到相邻的单元格【向下】或【向右】。她的路径将在田地的右侧或底部结束。
2. 爱丽丝的路径将田地分成两部分——一部分属于爱丽丝(包括她路径上的单元格),另一部分属于鲍勃。
3. 爱丽丝将拥有包括单元格 ($ n, 1 $) 的部分。
4. 鲍勃将拥有包括单元格 ($ 1, m $) 的部分。

爱丽丝希望以尽可能多的单元格的方式划分田地。

鲍勃希望保留所有喷泉的所有权,但他可以给爱丽丝一个。首先输出整数 $ \alpha $ —— 如果鲍勃不给爱丽丝任何喷泉,爱丽丝地块的最大可能大小(即所有喷泉都留在鲍勃的地块上)。

然后输出 $ k $ 个非负整数 $ a_1, a_2, \dots, a_k $,其中 $ a_i $ 是一个值,使得鲍勃给爱丽丝第 $ i $ 个喷泉后,爱丽丝地块的最大大小将是 $ \alpha + a_i $。

输入输出数据格式:
输入:
- 第一行包含一个整数 $ t $ ($ 1 \le t \le 10^4 $) —— 测试用例的数量。
- 每个测试用例的第一行包含三个整数 $ n $、$ m $ 和 $ k $ ($ 2 \le n, m \le 10^9 $, $ 2 \le k \le 2 \cdot 10^5 $) —— 田地的大小和喷泉的数量。
- 然后是 $ k $ 行,每行包含两个数字 $ r_i $ 和 $ c_i $ ($ 1 \le r_i \le n $, $ 1 \le c_i \le m $) —— 第 $ i $ 个喷泉所在单元格的坐标。保证所有单元格都不同,且没有一个单元格是 ($ n, 1 $)。
- 保证所有测试用例的 $ k $ 之和不超过 $ 2 \cdot 10^5 $。

输出:
- 对于每个测试用例,首先输出 $ \alpha $ —— 如果鲍勃不给爱丽丝任何喷泉,爱丽丝地块的最大大小。然后输出 $ k $ 个非负整数 $ a_1, a_2, \dots, a_k $,其中 $ a_i $ 是一个值,使得鲍勃给爱丽丝第 $ i $ 个喷泉后,爱丽丝地块的最大大小将是 $ \alpha + a_i $。

示例输入输出:
输入:
```
5
2 2 3
1 1
1 2
2 2
5 5 4
1 2
2 2
3 4
4 3
2 5 9
1 2
1 5
1 1
2 2
2 4
2 5
1 4
2 3
1 3
6 4 4
6 2
1 3
1 4
1 2
3 4 5
2 1
3 2
1 4
1 3
2 4
```
输出:
```
1
1 0 1
11
0 1 0 4
1
0 0 1 1 0 0 0 0 0
6
15 0 0 0
1
2 3 0 0 0
```题目大意: 这是一个较难版本的问题,与简单版本的区别仅在于问题的要求。简单版本只需要你打印某些值是否非零,而困难版本需要你打印确切的值。 爱丽丝和鲍勃正在划分一块田地。田地是一个大小为 $ n \times m $ ($ 2 \le n, m \le 10^9 $) 的矩形;行从上到下编号为 1 到 $ n $,列从左到右编号为 1 到 $ m $。行 $ r $ 和列 $ c $ 交叉的单元格表示为 ($ r, c $)。 鲍勃有 $ k $ ($ 2 \le k \le 2 \cdot 10^5 $) 个喷泉,它们都位于田地中的不同单元格。爱丽丝负责划分田地,但她必须满足几个条件: 1. 划分田地时,爱丽丝将从田地左侧或顶部的任意一个空闲(没有喷泉)单元格开始,每次移动到相邻的单元格【向下】或【向右】。她的路径将在田地的右侧或底部结束。 2. 爱丽丝的路径将田地分成两部分——一部分属于爱丽丝(包括她路径上的单元格),另一部分属于鲍勃。 3. 爱丽丝将拥有包括单元格 ($ n, 1 $) 的部分。 4. 鲍勃将拥有包括单元格 ($ 1, m $) 的部分。 爱丽丝希望以尽可能多的单元格的方式划分田地。 鲍勃希望保留所有喷泉的所有权,但他可以给爱丽丝一个。首先输出整数 $ \alpha $ —— 如果鲍勃不给爱丽丝任何喷泉,爱丽丝地块的最大可能大小(即所有喷泉都留在鲍勃的地块上)。 然后输出 $ k $ 个非负整数 $ a_1, a_2, \dots, a_k $,其中 $ a_i $ 是一个值,使得鲍勃给爱丽丝第 $ i $ 个喷泉后,爱丽丝地块的最大大小将是 $ \alpha + a_i $。 输入输出数据格式: 输入: - 第一行包含一个整数 $ t $ ($ 1 \le t \le 10^4 $) —— 测试用例的数量。 - 每个测试用例的第一行包含三个整数 $ n $、$ m $ 和 $ k $ ($ 2 \le n, m \le 10^9 $, $ 2 \le k \le 2 \cdot 10^5 $) —— 田地的大小和喷泉的数量。 - 然后是 $ k $ 行,每行包含两个数字 $ r_i $ 和 $ c_i $ ($ 1 \le r_i \le n $, $ 1 \le c_i \le m $) —— 第 $ i $ 个喷泉所在单元格的坐标。保证所有单元格都不同,且没有一个单元格是 ($ n, 1 $)。 - 保证所有测试用例的 $ k $ 之和不超过 $ 2 \cdot 10^5 $。 输出: - 对于每个测试用例,首先输出 $ \alpha $ —— 如果鲍勃不给爱丽丝任何喷泉,爱丽丝地块的最大大小。然后输出 $ k $ 个非负整数 $ a_1, a_2, \dots, a_k $,其中 $ a_i $ 是一个值,使得鲍勃给爱丽丝第 $ i $ 个喷泉后,爱丽丝地块的最大大小将是 $ \alpha + a_i $。 示例输入输出: 输入: ``` 5 2 2 3 1 1 1 2 2 2 5 5 4 1 2 2 2 3 4 4 3 2 5 9 1 2 1 5 1 1 2 2 2 4 2 5 1 4 2 3 1 3 6 4 4 6 2 1 3 1 4 1 2 3 4 5 2 1 3 2 1 4 1 3 2 4 ``` 输出: ``` 1 1 0 1 11 0 1 0 4 1 0 0 1 1 0 0 0 0 0 6 15 0 0 0 1 2 3 0 0 0 ```

加入题单

上一题 下一题 算法标签: