#P7344. 【DSOI 2021】积木

【DSOI 2021】积木

Description

“Now, what you have are some unit cubes with side length 11. And I, the owner of the game, will tell you the front view and the left view of the final 3D shape you build. You think I want you to build exactly that shape? Don’t panic, the game is far from that simple. In the front view, I will hide some columns, so you cannot observe their heights.

“To make the game more interesting, I rule that the heights of those columns you cannot observe are arbitrary—this can be decided by you.

“I suppose, my dear player, you have known since primary school that a shape cannot be uniquely determined with only the front view and the left view, not to mention that part of the front view is hidden by me. Therefore, let kk be the number of blocks that can be used to build a shape consistent with what I give. Please tell me: at most, how many integers kk can satisfy my requirements?” (That is, you should first decide the values of the unknown columns yourself, and then compute kk based on that choice. You need to choose the heights so that the number of possible values of kk is maximized.)

Input Format

This problem contains multiple test cases.
The first line contains an integer TT, the number of test cases.
For each test case:
The first line contains two integers n,mn,m, representing the length and width of the final block shape.
The second line contains nn integers. The ii-th integer aia_i is the height of blocks seen in the ii-th column from left to right in the front view. In particular, if ai=1a_i=-1, then the height of this column is arbitrary.
The third line contains mm integers. The ii-th integer bib_i is the height of blocks seen in the ii-th column from back to front in the left view. It is guaranteed that there is no column with unknown height.

Output Format

For each test case, output one integer per line, representing the maximum number of integers kk that can satisfy the requirements. In particular, if there is no integer kk that satisfies the conditions, output 00.

2
3 2
1 2 1
2 2
2 2
-1 2
2 2
3
5

Hint

[Sample Explanation]
For the first test case:
The feasible values of kk are 6,7,86,7,8.
For each value of kk, one possible construction is as follows (the top view is shown; the numbers indicate the height of that cell):

For the second test case:
When the heights of the unknown columns are set to 22, the number of valid kk values is maximized. There are 55 such values: 4,5,6,7,84,5,6,7,8.

[Constraints and Limits]
This problem uses bundled testdata. You must pass all test points in a Subtask to obtain the score for that Subtask.

Subtask Score n,m,ai,bin,m,a_i,b_i \le Special Property
1 7pts 33 None
2 8pts 1010 The number of 1-1 in array aa is at most 44
3 10001000 Guarantee ai1a_i \ne -1
4 12pts 2×1042\times10^4
5 7pts 10001000 Guarantee all bib_i are equal
6 8pts 2×1042\times10^4
7 15pts 10001000 None
8 35pts 2×1042\times10^4

For 100%100\% of the testdata, 1n,m2×1041 \le n,m \le 2\times10^4, 1ai2×104-1 \le a_i \le 2\times10^4, 0bi2×1040 \le b_i \le 2\times10^4, and 0T1000 \le T \le 100.

The input size of this problem is large. Please use a fast input method.

Translated by ChatGPT 5