TeamsCode Spring 2025 Advanced Division
A. Lily Pads
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Unfortunately, the sample output is not "NO". If that were the case, it would be impossible to AC at all!

Two frogs, Thomas and Waymo, are attempting to cross a pond.

Currently, they are both at the starting shore of the pond. In the pond, there are $$$N$$$ rows of unoccupied lily pads. In each row, there are two lily pads, a left lily pad and a right lily pad. One of them is afloat, and one of them is underwater. Also, each lily pad can hold at most one frog.

A frog may jump from the starting shore to an unoccupied and afloat lily pad in the first row, or from a lily pad in one row to an unoccupied and afloat lily pad in the next row, or from a lily pad in the last row to the ending shore.

When a frog jumps from a lily pad, that lily pad goes underwater, and the other lily pad in its row goes afloat. If a frog jumps from the right lily pad in a row, the right lily pad will go underwater, and the left lily pad will go afloat, and vice versa. Also, the frogs are polite. Thus, when one frog is mid-jump, the other frog will not jump.

Thomas and Waymo will jump in any order, obeying the above rules, until they both have made it to the ending shore. When a frog lands on a lily pad on the right, it gains a point. What is the maximum number of points total that Waymo and Thomas can gain?

Input

The first line contains $$$N$$$ ($$$1 \le N \le 100$$$), the number of rows of lily pads.

The second line contains a string $$$s$$$ with characters $$$s_1s_2 \dots s_n$$$. If $$$s_i = L$$$, the left lily pad on the $$$i$$$-th row is currently afloat, and if $$$s_i = R$$$, the right lily pad is currently afloat.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

Output the maximum number of points that Waymo and Thomas can earn.

Example
Input
1
R
Output
1
Note

A possible jumping order which gains 1 point is:

- Thomas jumps to row $$$1$$$. Thomas lands on the afloat right lily pad and gains a point.

- Thomas jumps to the ending shore. Now, in row $$$1$$$, the right lily pad is underwater, and the left lily pad is afloat.

- Waymo jumps to row $$$1$$$. Waymo lands on the now afloat left lily pad but does not gain a point.

- Waymo jumps to the ending shore. Now, in row $$$1$$$, the left lily pad is underwater, and the right lily pad is afloat.

After both frogs have reached the ending shore, they have gained $$$1$$$ point total.

Problem Idea: Alex_C

Problem Preparation: jay_jayjay

Occurrences: Novice A, Advanced A

B. Cell Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have been hired as a Temporary Part-Time Assistant Manager of the Executive Municipal Internet Service Provider Administration!

Your job is to set up cell towers to provide internet to homes in The City.

The City can be visualized as a very large rectangular grid. The rows are numbered starting from $$$1$$$, from south to north. The columns are numbered starting from $$$1$$$, from west to east. There are also $$$n$$$ houses on this grid, denoted by $$$(r, c)$$$, where $$$r$$$ is the number of the row the house is in, and $$$c$$$ is the number of the column the house is in.

You are trying to install cell towers for the houses. However, the cell towers have a peculiar set of rules.

A cell tower can only provide signal to a right isosceles triangle that extends south and east from it. More formally, a cell tower at $$$(r, c)$$$ covers all houses $$$(r', c')$$$, such that $$$c \leq c'$$$, and $$$r' + c' \leq r + c$$$.

If a cell tower is set at $$$(3, 1)$$$, it can only provide signal to houses at $$$(3, 1), (2, 1), (1, 1), (2, 2), (1, 2), (1, 3)$$$. Here is a diagram of the range of a cell tower at $$$(3, 1)$$$:

Because the terrain gets rougher the further north you go, the cost of a cell tower is equal to the number of the row it is installed. For instance, installing a cell tower at $$$(3, 1)$$$ has a cost of $$$3$$$.

A cell tower may be installed on top of houses, and it will still provide coverage as usual (despite protests from homeowners).

The City is a bit indecisive about how many cell towers they would like to install. Thus, for every $$$i$$$ from $$$1$$$ to $$$n$$$, find the minimum cost to cover all houses with at most $$$i$$$ cell towers.

Input

The first line contains $$$n$$$, denoting the number of houses. $$$(1 \leq n \leq 2 \cdot 10^5)$$$

The next $$$n$$$ lines contain $$$r, c$$$ $$$(1 \leq r \leq 10^9), (1 \leq c \leq 10^9)$$$, denoting a coordinate of a house.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Test $$$1$$$ satisfies $$$n = 1$$$.

Tests $$$2-8$$$ satisfy $$$n \leq 1000$$$, and for each house, $$$r, c \leq 1000$$$.

Tests $$$9-20$$$ satisfy no additional constraints.

Output

Output $$$n$$$ space separated integers, the $$$i$$$th of which is the minimum cost to cover all houses with at most $$$i$$$ cell towers.

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

Problem Idea: culver0412

Problem Preparation: culver0412

Occurrences: Novice I, Advanced B

C. Walk
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the following undirected graph, count the number of walks from $$$A$$$ to $$$A$$$ that visit nodes $$$A$$$, $$$B$$$, $$$C$$$, $$$D$$$ exactly $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ times, respectively, modulo $$$998244353$$$.

Note: A walk from $$$A$$$ to $$$A$$$ is a sequence of vertices $$$v_0 = A,v_1,…,v_k = A$$$ such that for each $$$i$$$ $$$(0 \leq i \lt k)$$$, there is an edge between $$$v_i$$$ and $$$v_{i+1}$$$. Two walks are considered distinct if they are distinct as sequences.
Input

The first line contains $$$t$$$ $$$(1 \leq t \leq 100)$$$, the number of independent test cases. The description of the test cases follows.

The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 \leq a$$$, $$$b$$$, $$$c$$$, $$$d \leq 5 \cdot 10^5$$$).

The sum of $$$a$$$, the sum of $$$b$$$, the sum of $$$c$$$, and the sum of $$$d$$$ across all test cases does not exceed $$$5 \cdot 10^5$$$.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

For each test case, print the number of valid walks modulo $$$998244353$$$.

Example
Input
2
3 1 2 1
1 2 1 2
Output
6
0
Note

In the first test case, the $$$6$$$ walks are $$$ABDCACA$$$, $$$ACDBACA$$$, $$$ACDCABA$$$, $$$ABACDCA$$$, $$$ACABDCA$$$, and $$$ACACDBA$$$.

In the second test case, it can be shown that there are no valid walks.

Problem Idea: HaccerKat

Problem Preparation: HaccerKat

Occurrences: Novice J, Advanced C

D. Not Japanese Triangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You encounter a triangle. The first thing you can tell about it is that it is most certainly not Japanese.

The triangle contains $$$n$$$ rows, the $$$i$$$th from top contains $$$i$$$ numbers, $$$a_{i,1}, a_{i,2} \dots a_{i,i}$$$. The numbers are not written in Japanese.

The triangle then speaks to you, but not in Japanese. It wagers $$$100$$$ points that you cannot find the sum of the numbers in each of its rows.

You confidently accept the challenge. But before you begin, the triangle puts on a not Japanese shirt, obscuring all rows from $$$1$$$ to $$$n-1$$$, and only the numbers in row $$$n$$$ remain visible.

Fortunately, the shirt has some not Japanese text that reveals an important property: Every hidden number is the minimum of two numbers directly below it. In other words, $$$a_{i,j} = min(a_{i+1,j}, a_{i+1, j+1})$$$ for all $$$1 \leq i \leq n-1$$$ and $$$1 \leq j \leq i$$$.

Can you find the sum of the not Japanese numbers in each not Japanese row of the not Japanese triangle?

Input

The first line contains one integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$.

The next line contains $$$n$$$ integers $$$a_{n,1}, a_{n,2} \dots a_{n,n}$$$ $$$(1 \leq a_{n,i} \leq 10^9)$$$, the numbers in the $$$n$$$th row of the not Japanese triangle.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-4$$$ satisfy $$$n \leq 5000$$$.

Tests $$$5-20$$$ satisfy no additional constraints.

Output

$$$n$$$ space separated integers, the $$$i$$$th integer representing the sum of the not Japanese numbers in the $$$i$$$th not Japanese row.

Examples
Input
6
1 2 1 2 2 6
Output
1 2 3 5 7 14 
Input
11
14 15 20 10 1 16 1 14 5 19 5
Output
1 2 3 4 5 6 7 21 39 58 120 
Note

In sample $$$1$$$, the rows are:

Row $$$1$$$: $$$[1]$$$, sum = $$$1$$$.

Row $$$2$$$: $$$[1, 1]$$$, sum = $$$2$$$.

Row $$$3$$$: $$$[1, 1, 1]$$$, sum = $$$3$$$.

Row $$$4$$$: $$$[1, 1, 1, 2]$$$, sum = $$$5$$$.

Row $$$5$$$: $$$[1, 1, 1, 2, 2]$$$, sum = $$$7$$$.

Row $$$6$$$: $$$[1, 2, 1, 2, 2, 6]$$$, sum = $$$14$$$.

Problem Idea: culver0412

Problem Preparation: jay_jayjay

Occurrences: Novice K, Advanced D

E. Robot Racing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Yam is racing robots! Unfortunately, his robots are out of control, so he has to stop them before they crash.

Yam has $$$N$$$ robots, each with speed $$$a_i$$$ ($$$a_i$$$ is non-decreasing). They are all positioned at the start (i.e., position $$$0$$$) of a track of length $$$L$$$ meters. For each subarray, Yam does the following:

First, he divides the robots in the subarray into $$$k$$$ groups of robots, where each robot must belong to exactly one group, and each group has at least one robot. Every second, every robot will advance $$$a_i$$$ meters forward. Then, after every second, Yam chooses a group and shoots every robot in the group, stopping them from moving forward in the future. If any robot crosses the end of the track (i.e., its position is strictly greater than $$$L$$$), then it spontaneously combusts. Define the score of this subarray as the maximum number of groups that Yam can form while ensuring that no robot spontaneously combusts. (Yam can choose to group the robots strategically to maximize the score.)

Yam thinks this is too easy, so he challenges you to find the sum of scores over all subarrays.

Input

The first line of input contains two integers $$$N$$$ and $$$L$$$ ($$$1\le N\le 2\cdot 10^5$$$, $$$1\le L\le 10^9$$$).

The following line contains $$$N$$$ integers $$$a_1, \dots, a_n$$$, ($$$1\le a_i\le L$$$).

Tests in subtasks are numbered from $$$1-20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-3$$$ satisfy $$$N\le 200$$$.

Tests $$$4-6$$$ satisfy $$$N\le 5000$$$.

Tests $$$7-20$$$ satisfy no additional constraints.

Output

Output the sum of scores over all subarrays.

Example
Input
4 10
1 3 6 10
Output
17
Note

The subarray $$$[1]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[1]$$$

The subarray $$$[3]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[3]$$$.

The subarray $$$[6]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[6]$$$.

The subarray $$$[10]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[10]$$$.

The subarray $$$[1,3]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[3]$$$, $$$[1]$$$ and shoots them in this order.

The subarray $$$[3,6]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[6]$$$, $$$[3]$$$.

The subarray $$$[6,10]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group, $$$[6, 10]$$$.

The subarray $$$[1,3,6]$$$ has score $$$3$$$ $$$-$$$ Yam creates 3 groups, $$$[6]$$$, $$$[3]$$$, $$$[1]$$$.

The subarray $$$[3,6,10]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[6, 10]$$$, $$$[3]$$$.

The subarray $$$[1,3,6,10]$$$ has score $$$3$$$ $$$-$$$ Yam creates 3 groups, $$$[6, 10]$$$, $$$[3]$$$, $$$[1]$$$.

Thus, the sum of scores is $$$1 + 1 + 1 + 1 + 2 + 2 + 1 + 3 + 2 + 3 = 17$$$.

Problem Idea: Yam

Problem Preparation: jay_jayjay

Occurrences: Novice L, Advanced E

F. Binary Function
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let $$$f(x)$$$ be the number of positive integers $$$y$$$ such that $$$y \lt x$$$ and $$$\text{popcount}(y) \gt \text{popcount}(x)$$$, where $$$\text{popcount}(z)$$$ is equal to the number of $$$1$$$s in the binary representation of $$$z$$$.

Given $$$t$$$ testcases of $$$x$$$ in its binary representation, find $$$f(x)$$$ for each test case. Because this number might be too large, output the answer modulo $$$998244353$$$.

Note: $$$|x|$$$ represents the length of $$$x$$$ in its binary representation. Also, some values of $$$x$$$ might be too large to convert to an integer, even with $$$64$$$ bits.

Input

The first line contains $$$t$$$ $$$(1 \leq t \leq 500)$$$, the number of testcases. The description of the test cases follows.

The only line of each test case contains the binary representation of an integer $$$x$$$ $$$(1 \leq |x| \leq 10^6)$$$.

The sum of $$$|x|$$$ across all test cases does not exceed $$$10^6$$$.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-2$$$ satisfy the sum of $$$|x|$$$ across all test cases does not exceed $$$20$$$.

Tests $$$3-6$$$ satisfy the sum of $$$|x|$$$ across all test cases does not exceed $$$5000$$$.

Tests $$$7-20$$$ satisfy no additional constraints.

Output

For each test case, print one integer — $$$f(x)$$$ modulo $$$998244353$$$.

Example
Input
4
1000
11
101011
11001000110
Output
4
0
1
664
Note

In the first test case of the sample, $$$1000$$$ in binary equals 8. $$$f(8) = 4$$$, since $$$3, 5, 6, 7$$$ are less than $$$8$$$, and $$$\text{popcount}(3) = 2, \text{popcount}(5) = 2, \text{popcount}(6) = 2, \text{popcount}(7) = 3$$$ are greater than $$$\text{popcount}(8) = 1$$$.

Problem Idea: Mustela_Erminea

Problem Preparation: HaccerKat

Occurrences: Advanced F

G. Binary Function II
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let $$$f(x)$$$ be the number of positive integers $$$y$$$ such that $$$y \lt x$$$ and $$$\text{popcount}(y) \gt \text{popcount}(x)$$$, where $$$\text{popcount}(z)$$$ is equal to the number of $$$1$$$s in the binary representation of $$$z$$$.

Given $$$l$$$, $$$r$$$ $$$(1 \leq l \leq r)$$$ in their binary representation, find $$$\sum_{x=l}^{r} f(x)$$$. Because this number might be too large, output the answer modulo $$$998244353$$$.

Note: $$$|x|$$$ represents the length of $$$x$$$ in its binary representation. Also, some values of $$$x$$$ might be too large to convert to an integer, even with $$$64$$$ bits.

Input

The first and only line of the input contains a value of $$$l$$$ and $$$r$$$ $$$(1 \leq |l| \leq |r| \leq 1000)$$$ separated by a space.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-2$$$ satisfy $$$|l|, |r| \leq 10$$$.

Tests $$$3-6$$$ satisfy $$$|l|, |r| \leq 100$$$.

Tests $$$7-20$$$ satisfy no additional constraints.

Output

Output the sum of $$$f(x)$$$ across the interval $$$x=l$$$ to $$$x=r$$$ inclusive modulo $$$998244353$$$.

Examples
Input
1000 1111
Output
8
Input
1 101011
Output
184
Input
11010 11001000110
Output
364182
Note

In the first sample, $$$l = 1000$$$ in binary equals $$$8$$$, and $$$r = 1111$$$ in binary equals $$$15$$$. $$$f(8) + f(9) + f(10) + f(11) + f(12) + f(13) + f(14) + f(15) = 4 + 1 + 1 + 0 + 2 + 0 + 0 + 0 = 8$$$.

Problem Idea: Mustela_Erminea

Problem Preparation: Mustela_Erminea

Occurrences: Advanced G

H. Permutation Composition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alex like math, and permutations are math. So he made a problem about permutations.

A permutation is a list of distinct integers from $$$1 \dots N$$$. Alex gives you two permutations, of length $$$N$$$: $$$a_1, a_2, \dots, a_N$$$ and $$$b_1, b_2, \dots, b_N$$$.

Alex is interested in permutation composition. Thus, he created two operations on the permutations:

Type 1 operation:

Compute $$$a'_i = b_{a_i}$$$ for all $$$1 \leq i \leq N$$$. Then, assign $$$a_i = a'_i$$$ for all $$$1 \leq i \leq N$$$.

Type 2 operation:

Compute $$$b'_i = a_{b_i}$$$ for all $$$1 \leq i \leq N$$$. Then, assign $$$b_i = b'_i$$$ for all $$$1 \leq i \leq N$$$.

He will then apply those operations $$$Q$$$ times and ask you to print out both permutations after all the operations.

However, Alex realized this problem was too hard for him to solve. So he decided to make it harder by making the operations more complicated:

Type 1 operation:

Compute $$$a'_i = a_{b_{a_i}}$$$ for all $$$1 \leq i \leq N$$$. Then, assign $$$a_i = a'_i$$$ for all $$$1 \leq i \leq N$$$.

Type 2 operation:

Compute $$$b'_i = b_{a_{b_i}}$$$ for all $$$1 \leq i \leq N$$$. Then, assign $$$b_i = b'_i$$$ for all $$$1 \leq i \leq N$$$.

Alex will now apply the new operations instead of the old operations to the permutations $$$Q$$$ times and asks you to print out both permutations after all the operations.

Input

The first line of input contains one integer $$$N \ (1 \leq N \leq 10^5)$$$.

The following line of input contains $$$N$$$ integers $$$a_1, a_2, \dots, a_N \ (1 \leq a_i \leq N)$$$. It is guaranteed $$$a$$$ is a permutation.

The following line of input contains $$$N$$$ integers $$$b_1, b_2, \dots, b_N \ (1 \leq b_i \leq N)$$$. It is guaranteed $$$b$$$ is a permutation.

The following line of input contains one integer $$$Q \ (1 \leq Q \leq 10^5)$$$.

The following $$$Q$$$ lines of input contain $$$1$$$, or $$$2$$$, specifying which type of operation is performed.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1−4$$$ satisfy $$$n \leq 5000$$$, $$$q \leq 5000$$$.

Tests $$$5-12$$$ satisfy $$$a_i = i$$$ for $$$1 \leq i \leq N$$$.

Tests $$$13-20$$$ satisfy no additional constraints.

Output

Output two lines.

The first line contains integers $$$a_1, a_2, \dots a_N$$$, representing $$$a$$$ after all the operations.

The second line contains integers $$$b_1, b_2, \dots b_N$$$, representing $$$b$$$ after all the operations.

Example
Input
5
1 3 4 2 5
5 4 3 1 2
2
1
2
Output
5 4 1 2 3 
3 4 5 2 1 
Note

In the sample, initially, $$$a = 1, 3, 4, 2, 5$$$, and $$$b = 5, 4, 3, 1, 2$$$

In the first operation, which is a type 1 operation, $$$a_{b_{a_1}} = 5$$$, $$$a_{b_{a_2}} = 4$$$, $$$a_{b_{a_3}} = 1$$$, $$$a_{b_{a_4}} = 2$$$, $$$a_{b_{a_5}} = 3$$$. Thus, $$$a$$$ becomes $$$5, 4, 1, 2, 3$$$, and $$$b = 5, 4, 3, 1, 2$$$ stays the same.

In the second operation, which is a type 2 operation, $$$b_{a_{b_1}} = 3$$$, $$$b_{a_{b_2}} = 4$$$, $$$b_{a_{b_3}} = 5$$$, $$$b_{a_{b_4}} = 2$$$, $$$b_{a_{b_5}} = 1$$$. Thus, after the operation, $$$a = 5, 4, 1, 2, 3$$$, and $$$b = 3, 4, 5, 2, 1$$$.

In the third operation, which is a type 2 operation, $$$b_{a_{b_1}} = 3$$$, $$$b_{a_{b_2}} = 4$$$, $$$b_{a_{b_3}} = 5$$$, $$$b_{a_{b_4}} = 2$$$, $$$b_{a_{b_5}} = 1$$$. Thus, after the operation, $$$a = 5, 4, 1, 2, 3$$$, and $$$b = 3, 4, 5, 2, 1$$$.

Thus, after all the operations $$$a = 5, 4, 1, 2, 3$$$, and $$$b = 3, 4, 5, 2, 1$$$.

Problem Idea: Alex_C

Problem Preparation: Alex_C

Occurrences: Advanced H

I. Permutation Online
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alex like math, and permutations are math. So he made a problem about permutations.

A permutation is a sequence of integers from $$$1$$$ to $$$N$$$ of length $$$N$$$ containing each number exactly once. You are given a permutation $$$p_1, p_2 \dots p_N$$$, and another list of integers $$$w_1, w_2 \dots w_K$$$.

For $$$1 \leq i \leq N$$$, and $$$1 \leq j \leq K$$$, define $$$a_{i, j} = k$$$ if $$$k$$$ is the $$$j$$$th largest index among those satisfying $$$p_k \gt p_i$$$ and $$$k \lt i$$$. If fewer than $$$j$$$ such indices satisfy the condition, $$$a_{i, j} = 0$$$.

For each index $$$1 \leq i \leq N$$$, please find $$$\sum_{j = 1}^K a_{i, j} w_j$$$.

For some test cases, you will need to process the permutation online. You must answer all the indices in order, and you won't know later values of the permutation until you've answered all the earlier indices.

Input

The first line contains three integers, $$$N$$$, $$$K$$$, and $$$O$$$, $$$1 \leq N, K \leq 10^6$$$, $$$1 \leq NK \leq 10^8$$$, and $$$0 \leq O \leq 1$$$.

The second line contains $$$K$$$ integers, $$$w_1, w_2, \dots w_K$$$, $$$1 \leq w_i \leq 10^6$$$ for all $$$1 \leq i \leq K$$$.

The third line contains $$$N$$$ integers.

If $$$O = 0$$$, this line contains $$$p_1, p_2, \dots p_N$$$.

If $$$O = 1$$$, this line contains $$$x_1, x_2 \dots x_N$$$, $$$1 \leq x_i \leq N$$$. The permutation is encrypted to ensure you process it online.

To decrypt $$$p_i$$$, use the formula

$$$$$$p_i = ((x_i + ans_{i-1})\mod N) + 1$$$$$$.

Where $$$ans_0 = 0$$$. Compute $$$p_1$$$, then find $$$ans_1$$$, then use $$$ans_1$$$ to compute $$$p_2$$$, and so on. —

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1−8$$$ satisfy $$$O = 0$$$.

Tests $$$9-20$$$ satisfy $$$O = 1$$$.

Tests $$$1-2$$$ and $$$9$$$ satisfy $$$N \leq 5000$$$, other tests satisfy no other constraints on $$$N$$$.

Tests $$$3-6$$$ and $$$10$$$ satisfy $$$K = 1$$$, other tests satisfy no other constraints on $$$K$$$.

Output

$$$N$$$ space-separated integers, representing the answer for each index from $$$1 \dots N$$$.

Examples
Input
5 2 0
1 10
5 2 4 1 3
Output
0 1 1 23 13 
Input
5 2 1
1 10
4 1 2 4 4
Output
0 1 1 23 13 
Note

In the first sample,

For $$$i = 1$$$, there are no indices $$$k$$$ satisfying $$$p_k \gt p_1$$$ and $$$k \lt 1$$$. Therefore $$$a_{1,1} = 0$$$ and $$$a_{1,2} = 0$$$, so the answer is $$$0 \cdot 1 + 0 \cdot 10 = 0$$$.

For $$$i = 2$$$, $$$k = 1$$$ is the only index satisfying $$$p_k \gt p_2$$$ and $$$k \lt 2$$$. Therefore $$$a_{2,1} = 1$$$ and $$$a_{2,2} = 0$$$, so the answer is $$$1 \cdot 1 + 0 \cdot 10 = 1$$$.

For $$$i = 3$$$, $$$k = 1$$$ is the only index satisfying $$$p_k \gt p_3$$$ and $$$k \lt 3$$$. Therefore $$$a_{3,1} = 1$$$ and $$$a_{3,2} = 0$$$, so the answer is $$$1 \cdot 1 + 0 \cdot 10 = 1$$$.

For $$$i = 4$$$, $$$k = 3, 2, 1$$$ are indices satisfying $$$p_k \gt p_4$$$ and $$$k \lt 4$$$. Therefore $$$a_{4,1} = 3$$$ and $$$a_{4,2} = 2$$$, so the answer is $$$3 \cdot 1 + 2 \cdot 10 = 23$$$. Note that even though $$$k = 1$$$ satisfies the condition, it is not used in the answer as $$$K = 2$$$, so only the 2 largest values of $$$k$$$ matter.

For $$$i = 5$$$, $$$k = 3, 1$$$ are indices satisfying $$$p_k \gt p_5$$$ and $$$k \lt 5$$$. $$$a_{5,1} = 3$$$ and $$$a_{5,2} = 1$$$, so the answer is $$$3 \cdot 1 + 1 \cdot 10 = 13$$$.

The second sample has the same values of $$$p$$$ as the first sample, but it is encrypted.

Problem Idea: Alex_C

Problem Preparation: Alex_C

Occurrences: Advanced I

J. Triangle Constructive
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The best way to approach any problem is from the try-angle.
— $$$\sin(Tzu)$$$, The $$$\Delta$$$rt of War

There is a triangular array with $$$n+1$$$ rows, which is an equilateral triangle pointing up. Each cell has a coordinate $$$(x, y, z)$$$ representing the number of rows it is away from the right slanted side, the bottom side, and the left slanted side respectively. All coordinates satisfy $$$x + y + z = N$$$ and $$$0 \leq x, y, z \leq N$$$.

Each cell contains a value which is initially $$$0$$$.

The triangular array when $$$N = 4$$$. The black triples are coordinates. The red numbers are values.

You can perform operations on the array.

In one operation, you can pick a number, then add that number to a parallelogram of cells which has one vertex coinciding with a vertex of the triangle and has edges which are aligned with the edges of the triangle adjacent to that vertex.

More formally, you can perform the following operations on the triangular array:

An operation of the form $$$X \ q_y \ q_z \ c$$$ will add $$$c$$$ to all cells $$$(x, y, z)$$$ such that $$$y \leq q_y$$$ and $$$z \leq q_z$$$. The restrictions on $$$q_y, q_z$$$ and $$$c$$$ are: $$$0 \leq q_y, q_z, q_y + q_z \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

An operation of the form $$$Y \ q_z \ q_x \ c$$$ will add $$$c$$$ to all cells $$$(x, y, z)$$$ such that $$$z \leq q_z$$$ and $$$x \leq q_x$$$. The restrictions on $$$q_z, q_x$$$ and $$$c$$$ are: $$$0 \leq q_z, q_x, q_z + q_x \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

An operation of the form $$$Z \ q_x \ q_y \ c$$$ will add $$$c$$$ to all cells $$$(x, y, z)$$$ such that $$$x \leq q_x$$$ and $$$y \leq q_y$$$. The restrictions on $$$q_x, q_y$$$ and $$$c$$$ are: $$$0 \leq q_x, q_y, q_x + q_y \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

For instance, after the operation $$$X \ 1 \ 2 \ 3$$$, the triangular array would look as follows:

The affected cells are the cells in the green parallelogram. ($$$y \leq 1$$$, and $$$z \leq 2$$$). Their values increase by $$$3$$$.

You are also given a triangular region $$$T$$$. Your objective is to use these operations to change the value of all cells in a given triangular region, $$$T$$$, into $$$1$$$, while leaving the rest of the triangular grid as $$$0$$$.

That triangular region will be specified by $$$q_x$$$, $$$q_y$$$, and $$$q_z$$$, $$$0 \leq q_x, q_y, q_z, q_x+q_y, q_y+q_z, q_z+q_x \leq N$$$.

If $$$q_x + q_y + q_z \leq N$$$, then $$$T$$$ contains all cells $$$(x, y, z)$$$ such that $$$x \geq q_x$$$, $$$y \geq q_y$$$, $$$z \geq q_z$$$.

If $$$q_x + q_y + q_z \gt N$$$, then $$$T$$$ contains all cells $$$(x, y, z)$$$ such that $$$x \leq q_x$$$, $$$y \leq q_y$$$, $$$z \leq q_z$$$.

For instance, if $$$q_x = 1$$$, $$$q_y = 1$$$, and $$$q_z = 1$$$, then $$$T$$$ would look as follows:

The purple triangle is $$$T$$$. The red values are the desired ending state of the triangular array.

And, if $$$q_x = 2$$$, $$$q_y = 2$$$, and $$$q_z = 2$$$, then $$$T$$$ would look as follows.

The purple triangle is $$$T$$$. Note that it is in the opposite orientation. The red values are the desired ending state of the triangular array.

Construct a sequence of at most $$$1000$$$ operations to accomplish the objective.

Input

One line containing $$$N$$$, $$$q_x$$$, $$$q_y$$$, and $$$q_z$$$. $$$(0 \leq N \leq 10^9, 0 \leq q_x, q_y, q_z, q_x+q_y, q_y+q_z, q_z+q_x \leq N)$$$.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-2$$$ satisfy $$$N \leq 15$$$.

Tests $$$3-7$$$ satisfy $$$N \leq 200$$$.

Tests $$$8-10$$$ satisfy $$$q_x, q_y, q_z = 0$$$.

Tests $$$11-20$$$ satisfy no additional constraints.

Output

Output $$$K$$$, the number of operations used. $$$0 \leq K \leq 1000$$$.

Then, output $$$K$$$ lines each contain an operation.

Operations are of the form:

$$$X \ q_y \ q_z \ c$$$, $$$0 \leq q_y, q_z, q_y + q_z \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

$$$Y \ q_z \ q_x \ c$$$, $$$0 \leq q_z, q_x, q_z + q_x \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

$$$Z \ q_x \ q_y \ c$$$, $$$0 \leq q_x, q_y, q_x + q_y \leq N$$$, and $$$-10^{15} \leq c \leq 10^{15}$$$.

Example
Input
2 1 1 1
Output
2
X 1 1 1
X 0 0 -1
Note

The array initially starts out in this state:

After the first operation, the values of cells $$$(2, 0, 0)$$$, $$$(1, 1, 0)$$$, $$$(1, 0, 1)$$$, $$$(0, 1, 1)$$$ increase by $$$1$$$.

After the second operation, the value of cell $$$(2, 0, 0)$$$ increases by $$$-1$$$. (or decreases by $$$1$$$)

Now, this completes the objective, as each cell in $$$T$$$: $$$(1, 1, 0)$$$, $$$(1, 0, 1)$$$, $$$(0, 1, 1)$$$ has value $$$1$$$, and each cell not in $$$T$$$ has value $$$0$$$.

Problem Idea: Alex_C

Problem Preparation: Alex_C

Occurrences: Advanced J

K. Bocchi the Tour
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After barely surviving her finals, Bocchi has decided to tour the country to do some sightseeing!

The country can be described by $$$n$$$ cities connected by $$$n - 1$$$ roads. The $$$i$$$-th road connects cities $$$u_i$$$ and $$$v_i$$$ with length $$$l_i$$$. It is possible to visit all the cities starting from any city using a sequence of roads.

Being an introvert, Bocchi hasn't seen much of the country, so she wants to visit as many cities as possible. However, she won't think a city is exciting if she has visited it within the last $$$k$$$ weeks. For example, if she visits some city on week $$$w,$$$ regardless of whether or not she thinks the city is exciting, she will not find the city exciting again before week $$$w+k$$$, at which point she will find it exciting again.

Bocchi is planning on going on $$$q$$$ tours. Specifically, for her $$$i$$$-th tour, she will start on week $$$w_i$$$ (there are many weeks in the strange land of Bocchi!) and start from city $$$u_i.$$$ But because Bocchi doesn't like being on the road for too long, she will travel to every city she can reach, as long as the length of each individual road does not exceed $$$s_i$$$. Each tour takes one week, and she can't be on multiple tours in the same week.

Bocchi is lazy and doesn't want to replan her tours. Please help her determine, for each tour, how many exciting cities she visits.

Input

The first line contains three integers $$$n, q, $$$ and $$$k$$$ $$$(1\leq n, q\leq 10^5, 1\leq k\leq 10^9)$$$ — the number of cities, the number of tours, and the amount of time before she thinks a city will become exciting again, respectively.

The next $$$n-1$$$ lines contain three integers $$$u_i, v_i, $$$ and $$$l_i$$$ $$$(1\leq u_i, v_i\leq n, 1\leq l_i\leq 10^9)$$$, representing a road of length $$$l_i$$$ connecting cities $$$u_i$$$ and $$$v_i$$$.

The next $$$q$$$ lines contain three integers $$$w_i, u_i, $$$ and $$$s_i$$$ $$$(1\leq u_i\leq n, 1\leq w_i, s_i\leq 10^9)$$$ — the week the $$$i$$$-th tour is on, the starting city, and the maximum length road Bocchi is willing to go through.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Test $$$1-2$$$ satisfies $$$n = 100$$$, $$$q = 100$$$, and $$$k\leq 1000$$$.

Test $$$3-10$$$ satisfies that there is a road between city $$$i$$$ and city $$$i+1$$$ for $$$1 \leq i \leq n-1$$$

Tests $$$11-20$$$ satisfy no additional constraints.

Output

For each of Bocchi's $$$Q$$$ tours, output a single integer — the number of exciting cities she visits.

Examples
Input
4 5 2
1 2 8
2 3 3
2 4 5
1 2 4
2 4 7
4 4 7
5 3 8
10 2 10
Output
2
1
3
1
4
Input
10 10 5
3 9 24
2 3 45
1 5 20
1 6 11
6 8 26
1 4 26
3 7 26
5 7 36
5 10 40
1 4 37
6 7 32
9 9 12
16 10 45
25 7 8
27 3 1
35 6 45
39 3 34
44 5 8
45 8 44
Output
8
3
0
10
1
1
10
0
1
8
Note

In the first sample:

In Bocchi's first tour on week $$$1$$$, she visits city $$$2$$$ and city $$$3$$$, both of which she finds exciting.

In Bocchi's second tour on week $$$2$$$, she visits cities $$$2$$$, $$$3$$$, and $$$4$$$, but she only finds city $$$4$$$ exciting.

In Bocchi's third tour on week $$$4$$$, she visits the same $$$3$$$ cities, and finds them all exciting.

Problem Idea: training4usaco

Problem Preparation: training4usaco

Occurrences: Advanced K

L. Can you Count?
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given positive integers $$$n,m,k$$$. Count the number of pairs of arrays $$$(a,b)$$$ consisting of only positive integers, such that:

  • $$$a$$$ has length $$$n$$$,
  • Each entry of $$$a$$$ is at most $$$m$$$,
  • $$$b$$$ has length $$$k$$$,
  • $$$\displaystyle \sum^n_{i=1} a=\displaystyle \max^k_{j=1} b$$$.
The answer can be large, so output it modulo $$$998244353$$$.

Since the above is too easy, there are $$$q$$$ queries. Each query specifies a $$$k$$$, and you have to answer the above. The values of $$$n$$$ and $$$m$$$ are fixed throughout the test case.

Input

The first line consists of three integers, $$$n,m,q$$$, $$$1\le n,m\le 9\cdot 10^8$$$ and $$$1\le q\le 10^5$$$.

The second line consists of $$$q$$$ integers, $$$k_1,k_2,\dots,k_q$$$, $$$1\le k_i\le 2\cdot 10^5$$$ denoting the value of $$$k$$$ in each query.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1−5$$$ satisfy $$$q=1, k_i\le 2\cdot 10^4$$$.

Tests $$$6-10$$$ satisfy $$$q=1$$$.

Tests $$$11-15$$$ satisfy $$$k_i\le 2\cdot 10^4$$$.

Tests $$$16-20$$$ satisfy no additional constraints.

Output

Output $$$q$$$ integers, the answer to each query, separated by spaces.

Examples
Input
2 2 4
1 2 3 4
Output
4 20 82 320 
Input
30624700 30624770 5
34202 139424 31 40 624
Output
962908989 896508782 487985302 35718864 495214615 
Note

Problem Idea: culver0412

Problem Preparation: culver0412

Occurrences: Advanced L