Esomer doesn't want to go to class at university, but he needs notes from his lectures to be able to study. Therefore, he has come up with the following system.
The university can be represented as a tree of $$$n$$$ nodes, and on a single day, Esomer will have $$$k$$$ classes on consecutive hours. Additionally, Esomer will hire note-takers. A note-taker can be hired at node $$$i$$$ at any time for a cost of $$$c_i$$$. Additionally, all note-takers can move to any neighbor node of their current node between hours. Note that Esomer can hire several note-takers at the same node and at the same time, and at any point in time, several note-takers can be at the same node.
Esomer's demand is simple, for each of his $$$k$$$ classes at nodes $$$v_1, v_2, \ldots, v_k$$$, there needs to be at least one note-taker at node $$$v_i$$$ at hour $$$i$$$, so that Esomer will get the notes for the class.
Unfortunately, Esomer is not rich, so he wants to pay the least possible amount in total, and he has asked for your help in calculating that quantity.
The first line contains a single integer $$$t$$$, the number of test cases $$$(1 \le t \le 50)$$$.
The first line of each test case consists of two integers $$$n$$$ and $$$k$$$, the number of nodes of the tree and the number of classes Esomer has to attend, respectively $$$(1 \le n \le 10^5, 1 \le k \le 200)$$$.
The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$, where $$$c_i$$$ denotes the cost of hiring a note-taker at node $$$i$$$ $$$(1 \le c_i \le 10^9)$$$.
Then follow $$$n-1$$$ lines, each with a pair of integers $$$a_i, b_i$$$, describing the edges of the tree $$$(1 \le a_i, b_i \le n, a_i \neq b_i)$$$.
Each test case ends with a line with $$$k$$$ integers, $$$v_1, v_2, \ldots, v_k$$$, such that Esomer needs to have a note-taker at $$$v_i$$$ at hour $$$i$$$ for all $$$1 \le i \le k$$$ $$$(1 \le v_i \le n)$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases is at most $$$10^5$$$ and that the sum of $$$k$$$ over all test cases is at most $$$200$$$.
For each test case, output a single integer equal to the minimum possible amount that Esomer must pay.
26 56 5 2 1 7 91 41 63 26 33 56 2 6 4 49 1010 3 3 9 1 10 5 8 13 76 17 68 64 78 92 79 53 9 5 9 9 4 4 5 8 8
12 5
We have a matrix with $$$n$$$ rows and $$$m$$$ columns, where all its values are initially set to $$$0$$$.
Additionally, we have $$$q$$$ operations of two types:
$$$*$$$ The first type of operation flips (changes $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$) an entire row or column.
$$$*$$$ The second type of operation queries the sum of a submatrix.
The top-left corner of the matrix corresponds to position $$$(1, 1)$$$, and the bottom-right corner corresponds to position $$$(n, m)$$$.
The first line contains a natural number $$$t$$$ $$$(1 \le t \le 10^5)$$$, which is the number of cases.
The first line of each case contains three integers $$$n, m, q$$$ $$$(1 \le n, m, q \le 10^5)$$$, where $$$n$$$ is the number of rows, $$$m$$$ the number of columns, and $$$q$$$ the number of operations.
For each query, the first number is $$$1$$$ if a row or column is flipped, or $$$2$$$ if the sum of a submatrix is requested.
If the query is of type $$$1$$$, two more numbers $$$c$$$ and $$$d$$$ are provided. If $$$c = 1$$$, a column will be flipped, and if $$$c = 2$$$, a row will be flipped. Finally, $$$d$$$ corresponds to the index of the row or column that will be flipped, $$$(1 \le d \le n)$$$ or $$$(1 \le d \le m)$$$.
Queries of type $$$2$$$ are given by four numbers $$$a, b, c, d$$$ where $$$(a, b)$$$ represents the top-left corner and $$$(c, d)$$$ the bottom-right corner of the submatrix, $$$(1 \le a \le c \le n)$$$ and $$$(1 \le b \le d \le m)$$$.
It is guaranteed that the total sum of $$$n, m$$$, and $$$q$$$ is less than $$$10^5$$$.
For each query of type $$$2$$$, you must respond with the sum of the specified submatrix.
23 5 82 1 1 3 51 1 32 1 1 3 51 1 52 1 1 3 51 1 42 1 1 3 52 1 2 2 43 5 52 1 1 1 11 2 31 1 41 1 52 1 1 3 4
0 3 6 9 4 0 5
It's your first time visiting Bratislava, and because your reputation as a good programmer precedes you, the mayor of Bratislava appoints you to solve a problem.
Bratislava has $$$n$$$ villages, but there are no roads connecting any two villages. In other words, you cannot travel from one village to another; each village is initially isolated. Building roads is expensive, so the mayor has a maximum budget that allows for constructing up to $$$m$$$ roads.
Your task is to minimize the maximum possible distance between all the villages.
The first line will consist of a single natural number $$$t$$$, ($$$1 \le t \le 10^5$$$) — the number of test cases.
The first line of each test case contains the natural numbers $$$n$$$ and $$$m$$$, ($$$2 \le n \le 5 \cdot 10^8$$$, $$$0 \le m \le 5 \cdot 10^8$$$).
For each test case, print the minimum maximum distance if Bratislava has $$$n$$$ villages and you can build $$$m$$$ roads. If it is impossible to connect all the villages, print $$$-1$$$.
33 24 811 2
2 1 -1
We have an infinite matrix $$$A$$$ defined as: $$$A_{i,j} = i + j$$$ for all $$$i, j$$$ such that $$$0 \le i, j$$$.
An infinite matrix $$$B$$$ is defined as: $$$\forall i,j$$$ such that $$$0 \le i$$$ and $$$1 \le j$$$ we have $$$B_{i,j}$$$ $$$=$$$ $$$A_{i,j-1}$$$ $$$\oplus$$$ $$$A_{i,j}$$$ $$$\oplus$$$ $$$A_{i,j+1}$$$.
Here $$$\oplus$$$ denotes the bitwise XOR operator.
If you are unfamiliar with XOR, please make sure to check out Esomer's XOR ultimate guide.
You are now given $$$q$$$ queries of the type $$${a, b, c, d}$$$. You are asked to find the total sum of all $$$B_{i,j}$$$ such that $$$a \le i \le c$$$ and $$$b \le j \le d$$$.
First line contains $$$q$$$, the number of queries $$$(1 \le q \le 2 \cdot 10^5)$$$.
Each query contains four numbers $$$a, b, c, d$$$, respectively. $$$(0 \le a \le c \le 2 \cdot 10^5)$$$, $$$(1 \le b \le d \le 2 \cdot 10^5)$$$.
Output for each query the total sum of all $$$B_{i,j}$$$ contained in the range.
61 1 1 12 2 2 23 3 4 42 3 5 60 1 2 31 3 5 10
0 2 28 128 29 380
Gridland is a mysterious world which exists inside of the Cartesian plane.
According to tradition, perhaps superstition, all the roads in Gridland must be made of linear segments parallel to the $$$x$$$ axis or the $$$y$$$ axis. The trains of Gridland have a very interesting property: they can only turn at most twice in one journey. This used to be an inconvenience for its citizens, who sometimes had to take more than one train in order to travel between two cities.
After a civil war broke out, the entire railway infrastructure of the country was destroyed. The new Minister of Transportation, Gustavo, has proposed an ambitious plan to build a new railway network that allows direct train travels between every city. However, Gustavo has to devise a budget to determine the project's feasibility. He needs your help to figure out the minimum total length of the new railway network.
The first line contains only an integer $$$t$$$ $$$(1 \le t \le 1000)$$$, the number of test cases.
The first line of each test case is an integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$, the number of cities in Gridland.
The second line of each test case contains $$$n$$$ pairs of integers $$$-10^9 \le x_i, y_i \le 10^9$$$, where $$$x_i$$$ and $$$y_i$$$ are the $$$x$$$ and $$$y$$$ coordinates, respectively, of the $$$i$$$-th city.
It is guaranteed that the total sum of $$$n$$$ from all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print one integer, the minimum length of roads needed to connect all of the cities such that it is always possible to travel between any two of them with at most two turns.
2 9 -3 0 -2 -2 -1 1 -1 4 0 0 1 3 2 -1 3 2 3 3 3 0 0 0 1 0 3
19 3
You are given $$$l$$$ and $$$r$$$ and you are asked to calculate the number pairs $$$(a, b)$$$ in that interval interval such that $$$a \lt b$$$, $$$a \oplus b \gt b$$$ and $$$l \le a, b \le r$$$.
Here $$$\oplus$$$ denotes XOR bitwise operation.
If you are unfamiliar with XOR, please visit Esomer's XOR ultimate guide.
The first line contains an integer $$$t$$$, $$$(1 \le t \le 10^5)$$$ — the number of test cases.
The first line of each test case contains two integers $$$l$$$, $$$r$$$, $$$(1 \le l \le r \le 10^9)$$$.
You should print the number of pairs that fulfil those conditions.
44 81 28 152 6
4 1 0 4
We are playing "Twister" with Echomel, but he is a dirty cheater, and we believe not all his limbs are inside the game panel.
You are given two pairs of integers representing the coordinates of the bottom-left and top-right corners of the Twister panel, respectively. The game panel is a rectangle.
Next, you will be given 4 pairs of integers, representing the coordinates where Echomel has placed his left hand, right hand, left foot, and right foot, respectively.
Note 1: Echomel is VERY flexible.
The first line contains an integer $$$t$$$, $$$(1 \le t \le 1000)$$$.
The first line for each case contains the coordinates of the rectangle $$$x1, y1, x2, y2$$$, $$$(-10^9 \le x1, y1, x2, y2 \le 10^9)$$$, $$$(x1 \lt x2)$$$ y $$$(y1 \lt y2)$$$.
The first line for each case contains the coordinates of Echomel's hands and feet $$$mi_x, mi_y, md_x, md_y$$$, $$$pi_x, pi_y, pd_x, pd_y$$$, $$$(-10^9 \le mi_x, mi_y, md_x, md_y$$$, $$$pi_x, pi_y, pd_x, pd_y \le 10^9)$$$ — The coordinates of each hand and foot.
$$$m = hand$$$, $$$p = foot$$$, $$$i = left$$$, $$$d = right$$$.
Print YES if Echomel is playing legitimately; otherwise, he will need to be punished, so print NO if Echomel is being a bad boy, both responses without quotes.
21 1 5 51 5 5 5 1 1 1 52 2 3 42 2 3 4 4 4 0 1
YES NO
Guadalajara is famous for its majestic mountains in the north and its well-known coastal town, Villanueva de la Torre. The regal, grand and dignifiedis danx is visiting Hectorungo, and during his visit, he will conduct a survey about the water in the alcarrian towns.
We have $$$2n-1$$$ towns, all associated with a river, organized in a tree structure where town $$$2n-1$$$ is the root.
In each town with index $$$i$$$ $$$(1 \leq i \leq n)$$$, a river originates with color $$$i$$$. Each town with index $$$j$$$ $$$(n+1 \leq j \leq 2n-1)$$$ connects two towns with smaller indices $$$a$$$ and $$$b$$$ $$$(a, b \lt j)$$$, and the rivers from these towns merge at $$$j$$$. The river of town $$$j$$$ will contain all the colors of the rivers from towns $$$a$$$ and $$$b$$$.
Each town has a population of $$$p_i$$$ inhabitants $$$(1 \leq p_i \leq 10^9)$$$.
Dani will ask $$$q$$$ questions. In each question, he will give a number $$$r$$$ followed by $$$r$$$ distinct numbers $$$c_1, c_2, \dots, c_r$$$ $$$(1 \leq c_i \leq n)$$$, where $$$c_i$$$ denotes a color.
Your task is to determine how many inhabitants live in towns whose river contains at least one of the colors given in the query.
The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$, indicating the number of test cases. Each test case is defined as follows:
An integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$, the number of towns where a river originates.
A list of $$$2n-1$$$ integers $$$p_1, p_2, \dots, p_{2n-1}$$$ $$$(1 \leq p_i \leq 10^9)$$$, where $$$p_i$$$ is the number of inhabitants in town $$$i$$$.
$$$n-1$$$ additional lines, where the $$$j$$$-th line $$$(n+1 \leq j \leq 2n-1)$$$ contains two integers $$$a$$$ and $$$b$$$ $$$(1 \leq a, b \lt j)$$$, representing the indices of the towns whose rivers merge at town $$$j$$$.
A line with an integer $$$q$$$ $$$(1 \leq q \leq 2 \cdot 10^5)$$$, indicating the number of queries.
$$$2q$$$ additional lines, where each line describes a query.
Each query contains on the first line an integer $$$r$$$ $$$(1 \leq r \leq 10^5)$$$. The second line of each query will contain $$$r$$$ distinct integers $$$c_1, c_2, \dots, c_r$$$ $$$(1 \leq c_i \leq n)$$$, representing the river colors to query.
The total sum of $$$r$$$ in all queries across all cases will not exceed $$$2 \cdot 10^5$$$ and the sum of $$$n$$$ across all casses will not exceed $$$1 \cdot 10^5$$$.
For each query, you must tell Dani how many inhabitants live in a town whose river contains at least one of the colors given in the query.
2 4 1 2 3 4 5 6 7 1 2 3 4 5 6 4 1 1 1 4 2 1 2 3 1 3 4 4 31 14 15 92 65 35 89 1 2 3 5 4 6 4 2 1 5 2 2 4 3 2 3 4 2 6 7
13 17 15 26 220 295 310 124
Litusiano was solving a problem during the prestigious CAMA competition (the best competition in the world) when he came up with the following problem:
In CAMA, all participants deserve to be happy. You are given an integer $$$n$$$ and an array of integers $$$a$$$ of length $$$n$$$. The $$$i$$$-th participant will be happy if they are in one of the first $$$a_i$$$ positions in the final ranking. Your task is to find the lexicographically smallest ranking such that all $$$n$$$ participants are happy, i.e., each participant $$$i$$$ is placed within the first $$$a_i$$$ positions of the ranking.
Lexicographical order means that, given two arrays $$$x$$$ and $$$y$$$ of the same length, $$$x$$$ is smaller than $$$y$$$ if, at the first position $$$k$$$ where they differ, $$$x_k \lt y_k$$$. For example: - $$$[1, 2, 3]$$$ is smaller than $$$[1, 3, 2]$$$ because at the second position $$$2 \lt 3$$$. - $$$[2, 1, 3]$$$ is greater than $$$[1, 3, 2]$$$ because at the first position $$$2 \gt 1$$$.
In the spirit of CAMA, where everyone should leave the competition with a smile, your task is to ensure that all participants are happy. If it's not possible to construct such a ranking, return -1.
The input consists of multiple test cases. The first integer $$$t$$$ ($$$1 \leq t \leq 100$$$) denotes the number of test cases. The description of each test case is as follows:
- The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^4$$$) — the number of participants. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the values of the array $$$a$$$, where each value represents the maximum position in the ranking where the $$$i$$$-th participant will be happy.
The sum of $$$n$$$ won't exceed $$$69 \cdot 10^4$$$.
For each test case, print the lexicographically smallest ranking such that all participants are happy. If it is not possible, print -1.
353 5 4 1 432 1 243 4 2 2
4 1 3 5 2 -1 3 4 1 2
After Héctor got his Architecture degree, he received a commission from Sheikh Artur. Since Artur is an extravagant individual, he has requested that his new house be shaped like a convex polygon with marble pillars in the countryside. He has also asked for a pillar at each vertex of the polygon, although he doesn't mind if there are extra columns inside that are not at the vertices.
However, Héctor forgot that the cottage is going to be built next to a lake with the shape of a convex polygon until after he had already placed all the marble columns, and the house he designs intersects with the lake. Obviously, he won't be able to build the cottage inside the lake, and since there's no time to redesign the building, Héctor has decided to improvise using the pillars that have already been placed. Help Héctor determine the maximum number of pillars that can be in Artur's house.
For this problem, you may consider a straight line segment or a single point as a convex polygon.
A few clarifications:
Even though some columns may not be in the water, part of the house built using them might end up inside the lake. This is not allowed.
Some of the vertices of the lake may be collinear.
We say that the house and the lake intersect only if their areas overlap. The house can be built even if its perimeter overlaps with the perimeter of the lake.
You are first given an integer $$$t$$$ ($$$1 \le t \le 10$$$), the number of test cases.
The first line of each test case consists of two positive integers $$$n$$$ ($$$3 \le n \le 100$$$) and $$$m$$$ ($$$1 \le m \le 10^4$$$), the number of vertices of the lake and the amount of pillars, respectively. These are followed by $$$n$$$ lines containing the coordinates $$$x_i$$$, $$$y_i$$$ of the $$$i$$$-th of the lake ($$$1 \le i \le n$$$), which will be given in clockwise order. It is guaranteed that the vertices in the given order form a convex polygon.
After that, there will be $$$m$$$ lines with the coordinates $$$a_j$$$, $$$b_j$$$ ($$$1 \le j \le m$$$) of the $$$j$$$-th pillar.
It is guaranteed that the sum of $$$m$$$ over all test cases is at most $$$10^4$$$.
Print an integer $$$x$$$, the maximum amount of pillars that Héctor can use to build the cottage while satisfying all of Sheikh Artur's requirements.
If it's impossible to build such a house, print 0.
1 3 4 2 -2 -2 3 0 4 3 -1 4 -3 1 -2 -1 -1
3
Litusiano was pooing when he came up with the following problem:
Given $$$l$$$ and $$$r$$$, you have to count how many dibonacci numbers are in the range $$$[l, r]$$$.
A dibonacci number is an integer whose digits are the sum modulo $$$10$$$ of the two previous digits.
For example, $$$123$$$ is a dibonacci number, because $$$(1 + 2)$$$ mod $$$10 = 3$$$, $$$471$$$ is also a dibonacci number because $$$(4 + 7)$$$ mod $$$10 = 1$$$ and $$$2169$$$ is not a dibonacci number because $$$(1 + 6)$$$ mod $$$10 = 7$$$, which is different from $$$9$$$.
Note that all the positive integers $$$\leq 99$$$ are also dibonacci numbers.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 69 \cdot 10^{4})$$$. The description of the test cases follows.
For each test case:
There is a line consisting of two integers $$$l, r$$$ $$$(1 \leq l \leq r \leq 10^{18})$$$ — the asked range.
For each test case, output how many dibonacci numbers are in the given range.
52 71 1246 9777337773 36969100000000 1000000000
6 102 452 49 90
In the first test case, the answer is $$$6$$$ because all the numbers in the range $$$[2, 7]$$$ are dibonacci numbers.
In the second case, there are $$$102$$$ dibonacci numbers in the range $$$[1, 124]$$$, like $$$101$$$ or $$$69$$$.
Litusiano was lying in the CAMA while looking at a photo of Emilio when he came up with the following problem:
You are given a permutation $$$p$$$ with some elements missing. Your task is to count how many ways you can fill those missing elements such that the result is still a valid permutation, and the permutation $$$q$$$ obtained by the following process:
For each $$$i$$$ from $$$1$$$ to $$$n$$$ (going from left to right), set $$$q[p[i]] = i$$$.
The resulting permutation $$$q$$$ must be the same as the original permutation $$$p$$$.
You are asked to output the value of the count modulo $$$998244353$$$.
Explanation of permutation:
A permutation is an arrangement of a set of elements in a specific order. For example, for the set $$$\{1, 2, 3\}$$$, the following are some permutations:
- $$$[1, 2, 3]$$$ - $$$[2, 1, 3]$$$ - $$$[3, 1, 2]$$$ - $$$[2, 3, 1]$$$
In this problem, a permutation is defined for a sequence of integers from $$$1$$$ to $$$n$$$. The goal is to fill in missing elements in $$$p$$$ in such a way that it remains a valid permutation and satisfies the given condition for $$$q$$$.
Each test case contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2024$$$) — the number of test cases.
For each test case:
- The first line consists of one integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^{4}$$$) — the length of the array $$$a$$$ and the permutation. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \leq a_i \leq n$$$) — the elements of the array $$$a$$$.
For each test case, output a single integer: the number of ways to fill the missing elements of the permutation $$$p$$$ such that the permutation $$$q$$$ obtained from the process:
For each $$$i$$$ from $$$1$$$ to $$$n$$$ (going from left to right), set $$$q[p[i]] = i$$$,
is the same as the original permutation $$$p$$$. The result should be output modulo $$$998244353$$$.
351 -1 4 3 -13-1 -1 -143 -1 4 -1
2 4 0
Litusiano was resting on his bed when he thought of the following problem:
You are given two integers, $$$n$$$ and $$$m$$$. Your task is to draw a letter "C" made of the character '#' with height $$$n$$$ and length $$$m$$$. After that, you need to print the word "AMA" at the bottom of the "C".
The "C" should have the following properties:
1. The "C" should have a height of $$$n$$$ rows. 2. The "C" should have a width of $$$m$$$ columns. 3. The top and bottom parts of the "C" should be fully filled with '#'. 4. The left part of the "C" should be a column of '#' characters, except for the top and bottom rows. 5. After printing the "C", print the word "AMA" in the next row.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 69$$$) — the number of test cases.
For each test case:
- The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 69$$$) — the height and length of the C of CAMA.
For each test case, print the "C" followed by the word "AMA" on the next line.
- The "C" should have a height of $$$n$$$ rows and a width of $$$m$$$ columns. - The top and bottom parts of the "C" should be fully filled with '#'. - The left part of the "C" should be a column of '#' characters, except for the top and bottom rows. - After printing the "C", print the word "AMA" on the next line.
54 12 36 92 61 9
# # # #AMA ### ###AMA ######### # # # # #########AMA ###### ######AMA #########AMA
Are you excited?
The first question of CAMA 2024
Tell the truth ;)
Are you excited?
Obviamente. Me gusta Esomer
Litusiano was listening to La Mafia Del Amor when he came up with the following problem:
You are given $$$n$$$ trees, where the $$$i$$$-th tree has $$$m_i$$$ nodes. You need to answer $$$q$$$ queries.
Each query consists of two integers $$$l$$$ and $$$r$$$. For each query, you are asked to find the minimum diameter of the resulting tree if you optimally merge all the trees in the range $$$[l, r]$$$.
To merge two trees, you add exactly one edge between a node from each tree. Refer to the sample explanation for a better understanding of the merging process.
Note: The diameter of a tree is defined as the longest path between any two nodes in the tree.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 2024$$$). The description of the test cases follows.
For each test case:
The first line consists of a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^{5}$$$) — the number of trees.
Then follows the representation of the $$$n$$$ trees. For each tree:
After the representation of all the trees, a line will follow with one integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^{5}$$$) — the number of queries.
The following $$$q$$$ lines will contain two integers $$$l$$$, $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) — the ranges for the queries.
It is guaranteed that the sum of $$$n$$$, $$$m_i$$$ and $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^{5}$$$.
For each query, output the minimum diameter of the resulting tree if you merge optimally all the trees in the range $$$[l, r]$$$.
2231 22 341 21 31 411 2121 211 1
4 2
In the first test case, the answer to the query is 4 because if you add an edge between node $$$2$$$ of the first tree and node $$$1$$$ of the second tree, the diameter is $$$4$$$, and it's impossible to merge both trees in such a way that results in a smaller diameter.
Litusiano was reading a book called Rich Dad Poor Dad when he came across the following problem:
You are given an integer $$$n$$$ and an array $$$a$$$ of length $$$n$$$. Your task is to find a permutation $$$p$$$ of length $$$n$$$ (i.e., an arrangement of the numbers from $$$1$$$ to $$$n$$$) that satisfies the following conditions:
1. For every index $$$i$$$ ($$$1 \leq i \leq n$$$): - Let $$$a_i$$$ be the smallest index greater than $$$i$$$ ($$$i \lt a_i \leq n$$$) such that $$$p_i \lt p_{a_i}$$$, or $$$a_i = -1$$$ if no such index exists. 2. The permutation $$$p$$$ must be the lexicographically smallest one that satisfies this property, or it must be determined that no such permutation exists.
Note on lexicographical order:
Lexicographical order (or lexicographically smallest) means that, given two arrays $$$x$$$ and $$$y$$$ of the same length, $$$x$$$ is smaller than $$$y$$$ in this order if at the first position $$$k$$$ where they differ, $$$x_k \lt y_k$$$. For example: - $$$[1, 2, 3]$$$ is smaller than $$$[1, 3, 2]$$$ because at the second position, $$$2 \lt 3$$$. - $$$[2, 1, 3]$$$ is greater than $$$[1, 3, 2]$$$ because at the first position, $$$2 \gt 1$$$.
If no permutation satisfying the given property exists, it should be explicitly stated.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2024$$$) — the number of test cases. The description of the test cases follows.
For each test case:
- The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^4$$$) — the length of the array $$$a$$$ and the permutation $$$p$$$. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \leq a_i \leq n$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$69 \cdot 10^4$$$.
For each test case, output the lexicographically smallest permutation that satisfies all the conditions or print "No hay PELODE :(", without the quotes, if such a permutation does not exist.
25-1 5 4 5 -15-1 4 5 5 -1
5 3 1 2 4 No hay PELODE :(
Rigo wants to take a bath, and for that, he needs to fill his bathtub. Rigo has $$$n$$$ buckets of water, each containing $$$p_i$$$ liters, where $$$p$$$ is a permutation of size $$$n$$$. Additionally, his bathtub can hold $$$2n-1$$$ liters if the water is cold and $$$2n+1$$$ liters if it is hot. Although Rigo is a very good mathematician, he is disorganized and has forgotten to label each bucket with its volume, so he asks for your help to assign each bucket its corresponding label in liters.
To do this, you can ask two types of questions:
Write a program that returns the permutation $$$p$$$ without running out of coins, making at most $$$2$$$ questions of type $$$2$$$ and $$$100000$$$ questions of type $$$1$$$.
The first line of the input contains a single integer $$$T$$$, the number of cases to solve. $$$(1 \le T \le 10)$$$.
Each case starts with a single integer $$$n$$$, the number of buckets, followed by the interaction. $$$(1 \le n \le 6000)$$$.
It is guaranteed that the sum of $$$n$$$ over all tests does not exceed $$$6000$$$.
To make a type $$$1$$$ question, the program must have the following format.
To make a type $$$2$$$ question, the format must be as follows:
Finally, to provide the answer, the program must print a single line in the following format:
If your program asks more than $$$2$$$ questions of type $$$2$$$ or $$$10^5$$$ questions of type $$$1$$$ for a test case, it must terminate immediately to receive the verdict Wrong Answer. Otherwise, you could receive an arbitrary verdict because your solution will attempt to continue reading from a closed stream.
Additionally, the sum of $$$m$$$ over all questions of type $$$1$$$ over all tests must not exceed $$$10^7$$$.
After printing a question or the answer, do not forget to write the end-of-line character and flush the output buffer. Otherwise, you might get the verdict Time limit exceeded. To do this, use:
1 5 1 0 1 3
2 3 1 3 F 1 4 2 1 3 C 1 4 2 2 4 ! 3 2 4 5 1
Note that, with the questions asked, you are not guaranteed that the answer is correct. This sample is just for illustrative effect.