The 2024 Sichuan Provincial Collegiate Programming Contest
A. Reverse Pairs Coloring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We know the definition of reverse pairs: for a permutation $$$a_1,a_2,\dots,a_n$$$ of length $$$n$$$, if there exists $$$i \lt j$$$ and $$$a_i \gt a_j$$$, then $$$a_i,a_j$$$ form a reverse pair. A permutation of length $$$n$$$ contains each of the numbers $$$1$$$ to $$$n$$$ exactly once.

Finding the number of reverse pairs should not be a difficult task for you. Now, you are given a permutation $$$a_1,a_2,\dots,a_n$$$ of length $$$n$$$. For each pair satisfying $$$i \lt j$$$ and $$$a_i \gt a_j$$$ of reverse pairs, color the cell in the $$$n \times n$$$ grid at row $$$i$$$ and column $$$a_j$$$. Please determine how many colored connected components are there in the final grid. Connected means that if two colored cells share an edge, then we consider these two cells to be connected.

Input

The first line contains an integer $$$n$$$ ($$$1 \le n \le 3 \times 10^5$$$), representing the length of the permutation.

The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$, representing a permutation of length $$$n$$$.

Output

Output an integer, representing the number of four-connected components in the final grid.

Examples
Input
9
5 9 1 8 2 6 4 7 3
Output
5
Input
2
1 2
Output
0

B. Link Summon
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are now in a magical land, and you have five different types of sprites around you. The ability value of the $$$i$$$-th sprite is $$$i$$$.

At this moment, a sprite summoner with a sprite of ability value $$$6$$$ passes by. This sprite with an ability value of $$$6$$$ looks very powerful, so you quickly ask him how to obtain this kind of sprite and obtain a method called "Link Summon" from him.

Link Summon: Each time, choose some sprites with ability values from $$$1$$$ to $$$5$$$. For each sprite, you can choose to assign a weight of $$$1$$$ or a weight of $$$x$$$ ($$$x$$$ is its ability value), and then obtain one sprite with the sum of the weights of these sprites. However, due to magical restrictions, the sum of the weights cannot exceed $$$6$$$.

Now you want to know how many sprites with an ability value of $$$6$$$ you can summon by linking the sprites in your possession.

Input

The first line contains an integer $$$T$$$ ($$$1\le T\le 10^5$$$), indicating the number of data sets.

Next are $$$T$$$ lines, each containing five integers $$$a_i$$$ ($$$0\le a_i \le 10^9,\sum a_i \le 10^9$$$), indicating the number of sprites you currently have with an ability value of $$$i$$$.

Output

For each set of data, output a single integer on a line, indicating the maximum number of sprites with an ability value of $$$6$$$ that you can summon through linking.

Example
Input
5
3 3 3 3 3
2 3 4 5 1
1 2 3 4 5
2 2 0 0 0
0 3 0 0 3
Output
7
7
7
1
3

C. Black-White Cubic Lattice
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given a cubic lattice of size $$$N \times M\times L$$$ such that each lattice point $$$(i,j,k)$$$ is either white or black. The coordinates of the lattice point are all positive integers. Let the cost of flipping the color of lattice point $$$(i,j,k)$$$ be $$$a_{i,j,k}$$$. Find the minimum cost to flip the colors of lattice points such that the bottom left corner $$$(1,1,1)$$$ is black, the top right corner $$$(N,M,L)$$$ is white, and any two different lattice points $$$(i_1, j_1, k_1)$$$ and $$$(i_2,j_2,k_2)$$$ satisfy at least one of the following conditions.

  • $$$i_1 \gt i_2$$$
  • $$$j_1 \gt j_2$$$
  • $$$k_1 \gt k_2$$$
  • $$$(i_1, j_1, k_1)$$$ is black.
  • $$$(i_2, j_2, k_2)$$$ is white.
Input

The first line includes three integers $$$N, M, L$$$ ($$$1\le N, M, L\le 5\times 10^3$$$, $$$2 \leq N \times M \times L \leq 5\times 10^3$$$), denoting the size of the lattice. Each of the next $$$L \times N$$$ lines contains a string of length $$$M$$$ that only consists of B and W. The initial color of lattice point $$$(i,j,k)$$$ is the $$$j$$$-th character of the $$$(k-1) \times N + i$$$ line. If the character is B, it means the color of the lattice point is black, otherwise it is white. Each of the next $$$L \times N$$$ lines contains $$$M$$$ non-negative integers not greater than $$$10^5$$$. The cost of flipping the color of lattice point $$$(i,j,k)$$$ is the $$$j$$$-th integer of the $$$(k-1) \times N + i$$$ line.

Output

Output the minimum cost.

Example
Input
2 2 2
WW
WW
BB
BB
1 1
1 1
2 2
2 2
Output
5

D. L-Covering
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given a grid with $$$n$$$ rows and $$$m$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Now there are infinite $$$1+1$$$ L-shape tiles (as shown in the figure below), you need to use these tiles to cover the grid, so that only the cell in the $$$1$$$-st row and $$$m$$$-th column (located at the top right corner) is not covered, and each other cell is covered by exactly one tile. You need to determine if it is possible to achieve a covering that satisfies these conditions, and if so, output one covering scheme.

Input

The first line contains an integer $$$T$$$ ($$$1\le T\le 10^4$$$), indicating the number of test cases.

For each test case, there is a line containing two integers $$$n,m$$$ ($$$2\le n,m\le 500$$$), representing the size of the grid to be covered.

It is guaranteed that the sum of all $$$n\times m$$$ for the test cases does not exceed $$$10^6$$$.

Output

For each test case, if it is not possible to achieve a covering that satisfies the conditions, output No on a single line.

Otherwise, first output Yes on a single line, then output $$$n$$$ lines, each containing a string of length $$$m$$$, representing one covering scheme. The string contains only the six characters UDLRC.. The $$$j$$$-th character in the $$$i$$$-th line represents the covering situation of the cell in the $$$i$$$-th row and $$$j$$$-th column of the grid. The character . represents a cell that is not covered. For the output covering scheme, there should be only one . located in the $$$1$$$-st row and $$$m$$$-th column. The character C represents the center of the tile (i.e., the bottom left corner of the tile). The characters UDLR respectively represent the fact that the top, bottom, left, right cell of this cell is covered by the center of the tile. You should ensure that each cell is covered by only one tile, except for the top right corner cell.

Example
Input
2
4 4
2 3
Output
Yes
CLD.
UDCL
DCLD
CLRC
No
Note

For the first test case, one possible covering scheme is shown in the figure below.

E. L-Covering Checker
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In this problem, you need to implement the output checker from the previous problem.

Given a grid with $$$n$$$ rows and $$$m$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Now, there are infinite $$$1+1$$$ L-shaped tiles (as shown in the figure below), and you have used some tiles to cover the grid, but you do not know if you have covered the grid correctly.

Your covering scheme can be represented as a string of length $$$m$$$ for each of the $$$n$$$ rows. The string contains only the six characters UDLRC., where the $$$j$$$-th character of the $$$i$$$-th row represents the covering situation of the grid in the $$$i$$$-th row and the $$$j$$$-th column. The character . represents that the cell is not covered. If there is only one . in the covering scheme, and this . is located in the $$$1$$$-st row and the $$$m$$$-th column, then the covering scheme is correct; otherwise, it is incorrect. The character C represents the center of the tile (i.e., the bottom-left corner of the tile in the figure). The characters UDLR respectively represent that the top, bottom, left, right cell of this cell is covered by the center of the tile. If all L-shaped tiles are complete and each cell is covered by only one tile, then the covering scheme is correct; otherwise, it is incorrect.

Given some covering schemes, please determine if these schemes are correct.

Input

The first line contains an integer $$$T$$$ ($$$1\le T\le 10^4$$$), indicating the number of test cases.

For each test case, the first line contains two integers $$$n,m$$$ ($$$2\le n,m\le 500$$$), indicating the size of the grid.

Then, there are $$$n$$$ lines, each containing a string of length $$$m$$$, where the string consists of only the six characters UDLRC.. The $$$j$$$-th character of the $$$i$$$-th line string represents the covering situation of the cell in the $$$i$$$-th row and the $$$j$$$-th column of the grid.

It is guaranteed that the sum of all $$$n\times m$$$ for all test cases does not exceed $$$10^6$$$.

Output

For each test case, if the covering scheme is correct, output Yes; otherwise, output No.

Example
Input
2
4 4
CLD.
UDCL
DCLD
CLRC
2 3
DRC
CLU
Output
Yes
No

F. Isoball: 2D Version
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Isoball is a challenging and fun game, and in this problem, we consider its 2D version. Given the radius of a circle and its initial position, there is a rectangle on the plane, and your goal is to let the circle be in the interior of the rectangle. To do this, you determine the direction of movement for the circle (represented by a vector). Now, you want to know if there exists a moment (including the initial moment) when the circle moves in this direction to reach the goal.

For a circle with center at $$$(p,q)$$$ and radius $$$r$$$, it is considered to be inside a rectangle with bottom-left corner at $$$(l_x,l_y)$$$ and top-right corner at $$$(r_x,r_y)$$$ if and only if $$$\forall p\in\{(x,y)\mid (x-p)^2+(y-q)^2\le r^2\}$$$, it holds that $$$l_x\le p_x\le r_x$$$ and $$$l_y\le p_y\le r_y$$$, where $$$p_x,p_y$$$ are the horizontal and vertical coordinates of point $$$p$$$, respectively.

Input

The first line contains an integer $$$T$$$ ($$$1\le T\le 10^4$$$), indicating the number of test cases.

For each test case, the first line contains five integers $$$x,y,r,v_x,v_y$$$ ($$$-10^6\le x,y,v_x,v_y\le 10^6$$$, $$$1\le r\le 10^6$$$), representing the initial horizontal and vertical coordinates of the circle's center, the radius of the circle, and the direction of movement. It's guaranteed that both $$$v_x$$$ and $$$v_y$$$ are not simultaneously equal to $$$0$$$.

The second line contains four integers $$$l_x,l_y,r_x,r_y$$$ ($$$-10^6\le l_x,l_y,r_x,r_y\le 10^6$$$), representing the horizontal and vertical coordinates of the bottom-left and top-right corners of the rectangle. It's guaranteed that $$$l_x \lt r_x$$$ and $$$l_y \lt r_y$$$.

Output

For each test case, output one line. If the goal can be achieved, output Yes; otherwise, output No.

Example
Input
5
0 0 1 1 0
2 -2 6 2
0 0 1 1 0
2 0 6 2
0 0 1 1 1
1 1 3 3
0 0 1 -1 -1
1 1 3 3
0 0 1 -1 1
-5 -5 5 5
Output
Yes
No
Yes
No
Yes

G. Function Query
time limit per test
2 seconds
memory limit per test
256 MB
input
standard input
output
standard output

Given $$$n$$$ integers $$$x_1,x_2,\ldots,x_n$$$. There are $$$q$$$ queries, each query gives a function $$$f(x)=(a\oplus x)-b$$$, please determine if there exists $$$1\le i \lt n$$$ such that $$$f(x_i)\cdot f(x_{i+1})\le 0$$$, if so, output a satisfying $$$i$$$, otherwise output $$$-1$$$.

$$$a\oplus b$$$ represents the bitwise XOR operation between $$$a$$$ and $$$b$$$.

Input

The first line contains two integers $$$n,q$$$ ($$$2\le n\le 3\cdot 10^5$$$, $$$1\le q\le 3\cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$x_1,x_2,\ldots,x_n$$$ ($$$0\le x_i\le 10^9$$$).

Following are $$$q$$$ lines, each line contains two integers $$$a,b$$$ ($$$0\le a,b\le 10^9$$$).

Output

Output $$$q$$$ lines, representing the answer for each query.

Example
Input
5 6
3 5 1 2 4
0 2
1 1
2 3
3 2
4 2
5 8
Output
2
3
2
1
4
-1

H. GG and YY's Stone Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

GG and YY are playing a game on a pile of stones of size $$$n$$$. GG and YY play in turns. GG moves first. In each turn, the corresponding player can remove 1 or 2 stones from the pile. The player who cannot move loses. Both players want to win and get as many stones as possible. Assume GG and YY both follow the optimal strategy. Please decide the winner and answer the number of stones, $$$v$$$, that the winner gets at the end.

Input

The first line contains one integer $$$T$$$ ($$$1\le T\le 10^4$$$), indicating the number of test cases.

Each test case contains one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$) in one line, indicating the number of stones.

Output

For each test case, if GG wins, output "0 $$$v$$$", otherwise output "1 $$$v$$$" (without quotes).

Example
Input
3
1
2
3
Output
0 1
0 2
1 1

I. Container Scheduling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a busy port, a large number of containers need to be stacked on the deck of cargo ships every day. For convenience, you can consider both the deck and the containers as rectangles parallel to the coordinate axes. The deck is a large rectangular area with the lower left corner at $$$(0,0)$$$ and the upper right corner at $$$(l,h)$$$.

You are a container scheduler and need to arrange the position of each container on the deck. In theory, you need to schedule it reasonably to maximize space utilization. However, you feel that you are underpaid, so you want to be lazy in the scheduling method. The scheduling method you provide is as follows:

  • Due to height restrictions, containers can only be stacked in a single layer without overlapping.
  • Since you don't have the budget to buy rotating cranes, containers cannot be rotated when placed.
  • Containers follow the first-come, first-served order, meaning that containers at the front are placed first, and containers at the back are placed later, without changing the order of the containers. If a container cannot be placed during the process, it is discarded.
  • When selecting a position to place a container, prioritize the position with the smallest $$$x$$$ coordinate that can accommodate it. If there are multiple candidate positions, choose the one with the smallest $$$y$$$ coordinate.

There are a total of $$$n$$$ containers, and for each container, the length along the $$$x$$$ axis and the width along the $$$y$$$ axis are given in order. Your task is to determine whether each container can be placed, and if so, provide the position of its lower left corner.

Input

The first line contains three integers $$$n,l,h$$$ ($$$1 \le n \le 50$$$, $$$1 \le l,h \le 10^9$$$), representing the number of containers and the length and width of the deck area, respectively.

The next $$$n$$$ lines each contain two integers $$$x,y$$$ ($$$1 \le x,y \le 10^9$$$), representing the length along the $$$x$$$ axis and the width along the $$$y$$$ axis of each container.

Output

For each container in order, output the coordinates of its lower left corner separated by a space if the container can be placed. Otherwise, output $$$-1$$$.

Example
Input
4 10 10
5 5
6 6
4 7
10 2
Output
0 0
-1
5 0
0 7

J. Roman Numerals
time limit per test
1 s
memory limit per test
256 megabytes
input
standard input
output
standard output

There are seven characters in Roman numerals: I, V, X, L, C, D, M, representing $$$1$$$, $$$5$$$, $$$10$$$, $$$50$$$, $$$100$$$, $$$500$$$, $$$1000$$$ respectively.

Now you will use Roman numerals and Arabic numerals to represent a positive integer in decimal. For example, $$$\texttt{X}2 = 10\times 10^1 + 2 \times 10^0=102$$$, $$$\texttt{IV}=1\times 10^1+5\times 10^0=15$$$.

For each digit, there is a certain cost for its representation. Find the minimum cost to represent a positive integer using a mixed representation.

Input

The first line contains a positive integer $$$T$$$ ($$$1\le T\le 2\cdot 10^3$$$), indicating the number of test cases.

For each test case, the first line contains a positive integer $$$n$$$ ($$$1\le n\le 10^{18}$$$), indicating the integer to be represented.

The second line contains 10 positive integers $$$c_0,c_1,\ldots,c_9$$$ ($$$1\le c_i\le 10^7$$$), representing the cost of using the Arabic numerals $$$0$$$ to $$$9$$$ in the mixed representation.

The third line contains 7 positive integers $$$c_{\texttt{I}},c_{\texttt{V}},\ldots,c_{\texttt{M}}$$$ ($$$1\le c_i\le 10^7$$$), representing the cost of using the Roman numerals I to M in the mixed representation.

Output

For each test case, output a single integer, representing the minimum cost required to represent the integer in decimal using a mixed representation of Roman numerals and Arabic numerals.

Example
Input
5
102
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
112
1 5 5 1 1 1 1 1 1 1
1 1 1 1 1 1 1
150
1 1 1 1 1 1 1 1 1 1
5 5 5 5 5 5 5
114514
10 5 5 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1919810
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
Output
2
7
3
6
6
Note

$$$102$$$ can be represented as $$$\texttt{X}2$$$, $$$\texttt{I}02$$$, and $$$102$$$. The representation with the minimum cost is $$$\texttt{X}2$$$, requiring a cost of $$$2$$$.

$$$112$$$ can be represented as $$$\texttt{I}12$$$, $$$1\texttt{I}2$$$, $$$\texttt{II}2$$$ and $$$112$$$. The representation with the minimum cost is $$$\texttt{II}2$$$, requiring a cost of $$$7$$$.

$$$150$$$ can be represented as $$$\texttt{XL}$$$, $$$\texttt{5C}$$$, $$$10\texttt{L}$$$, etc. The representation with the minimum cost is $$$150$$$, requiring a cost of $$$3$$$.

K. Element Reaction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You now have $$$m$$$ types of elements. The name of the first element is a, the second one is b, and so on. You will use these elements to attack monsters.

Specifically, you have a sequence of length $$$n$$$ for using elements, and you will use these elements to attack monsters in order. Assuming the current type of element is $$$x$$$, if the monster does not have an element attached to it, then the element $$$x$$$ will be attached to the monster. If the monster already has a certain element $$$y$$$, then the two elements will react, causing damage of $$$a_{y,x}$$$. After the reaction, only the element $$$x$$$ will remain on the monster, and reactions will also occur between elements of the same type.

However, now some types of elements may have become inert, meaning that these elements will not react or attach to the monster when used to attack, and will be ignored directly.

But you do not know which types of elements have become inert, so you want to know for each type of element, in the $$$2^m$$$ possible scenarios of whether it is inert or not, the total damage caused by the element sequence.

Input

The first line contains two integers $$$m,n$$$ ($$$1\le n\le 10^5,1\le m\le 17$$$).

The next $$$m$$$ lines each contain $$$m$$$ integers, where the $$$j$$$-th number in the $$$i$$$-th line represents the reaction damage $$$a_{i,j}$$$ when the $$$i$$$-th type of element is used before the $$$j$$$-th type of element ($$$0\le a_{i,j}\le 10^8$$$).

The next line contains a string of length $$$n$$$, representing the given element sequence. It is guaranteed that only the first $$$m$$$ lowercase English letters appear in the string.

Output

Output a line of $$$2^m$$$ integers, where the $$$i$$$-th number represents the total damage caused by the element sequence when all elements are considered inert as $$$1$$$ and non-inert as $$$0$$$, and the binary number obtained by arranging the element types in ascending order from the least significant bit to the most significant bit is $$$i-1$$$.

Examples
Input
3 4
1 2 3
4 5 6
7 8 9
abca
Output
15 6 10 0 6 0 1 0 
Input
3 10
1 2 3
4 5 6
7 8 9
acbabccbac
Output
47 42 32 27 17 10 2 0 

L. Beef Tripe in Soup Pot?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Students at the Trellis Hot Pot School are currently engaged in a heated discussion about which ingredients can be cooked in which type of hot pot.

There are two types of hot pots: spicy hot pots and soup pots. There are $$$n$$$ types of ingredients, numbered from $$$1$$$ to $$$n$$$. The cooking times for the $$$i$$$-th ingredient in the spicy hot pot and soup pot are denoted as $$$a_i$$$ and $$$b_i$$$, respectively (it is guaranteed that all $$$a_i$$$ and $$$b_i$$$ are distinct). Based on the discussion results, they have determined which ingredients can be cooked in each type of hot pot.

The students at the Trellis Hot Pot School have a unique way of eating hot pots. They will wait until all $$$n$$$ types of ingredients are ready and then decide whether each ingredient should be placed in the spicy hot pot or soup pot. If an ingredient can be placed in both pots, they will choose to place it in the pot with the shorter cooking time. After the discussion, all ingredients are put into the regarding pot. Due to the exhausting discussion process, when an ingredient is cooked, they will immediately fish it out and eat it.

The students at the Trellis Hot Pot School want to know the order in which the ingredients are fished out from the spicy hot pot and soup pot, respectively. Please output the answers in the order of fishing out time.

Input

The first line contains an integer $$$n$$$ ($$$2\le n \le 10^5$$$), indicating the number of types of ingredients.

For the next $$$n$$$ lines, the $$$i$$$-th line describes the $$$i$$$-th type of ingredients, which contains four integers representing the cooking time $$$a_i$$$ in the spicy hot pot, cooking time $$$b_i$$$ in the soup pot, whether the ingredient can be cooked in the spicy hot pot $$$c_i$$$, and whether the ingredient can be cooked in the soup pot $$$d_i$$$. Here, $$$1\le a_i,b_i\le 2\times n$$$, $$$c_i,d_i\in \{0,1\}$$$. It is guaranteed that each ingredient can be cooked in at least one type of hotpot, and for any $$$1\le i,j\le n$$$, it is guaranteed that $$$a_i \neq a_j$$$, $$$b_i \neq b_j$$$, and $$$a_i \neq b_j$$$.

Output

Output two lines.

The first line should start with a number $$$k$$$, representing the number of ingredients cooked in the spicy hot pot, followed by $$$k$$$ integers representing the type of the ingredients in the spicy hot pot, sorted by cooking time, separated by spaces.

The second line should start with a number $$$c$$$, representing the number of ingredients cooked in the soup pot, followed by $$$c$$$ integers representing the type of the ingredients in the soup pot, sorted by cooking time, separated by spaces.

Example
Input
8
9 11 0 1
1 8 0 1
15 7 1 0
3 13 1 1
6 12 0 1
16 5 1 0
4 2 1 0
10 14 1 0
Output
5 4 7 8 3 6
3 2 1 5