In a faraway school early in the morning. The school manager asked the $$$n$$$ students to line up in a row at the top of the wall.
They lined up one after the other according to the time of their arrival. Each student has a length of $$$h_i$$$ centimeters.
Meanwhile, there was a student named Levi who was angry because he was the shortest among the students. When he was asked about the reason for his anger, it became clear that he did not want to be the shortest person between the person in front of him and the person behind him.
The manager decided to make everyone happy without changing their places, by removing stones below any student, or even by adding stones under any student. Each stone is only one centimeter long, so when a stone is added, the person becomes one centimeter taller, and vice versa when removed.
Adding a stone anywhere costs $$$x$$$ energy units, and removing a stone anywhere costs $$$y$$$ energy units.
You can consider the length of the wall to be infinite and you can remove any amount of stones you want.
You can consider that the first and last students are always happy.
You need to find the minimum cost to make all students happy.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers $$$n,x,y$$$ $$$(1 \le n \le 10^5, 0 \le x,y \le 10^9)$$$ — the number of students, the cost of adding a stone, and the cost of removing a stone, respectively.
The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$ $$$(0 \le h_i \le 5 \cdot 10^3)$$$ — the length of each student.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the minimum cost to make all students happy.
24 5 51 6 2 56 2 45 4 5 5 4 6
15 4
In the first test case, the student standing in place $$$3$$$ is sad because he is shorter than the one in front of him in place $$$4$$$ and the one behind him in place $$$2$$$, to make him happy we will remove $$$3$$$ stones from the person in front of him. Thus, the lengths become as follows $$$h=[1,6,2,2]$$$ and everyone becomes happy.
In the second test case, students $$$2$$$ and $$$5$$$ are sad, so to make them happy at the lowest cost, we will add a stone below each one of them, then the lengths become as follows $$$h=[5,5,5,5,5,6]$$$.
Shaaban found the contestants eager to participate in the competition and solve educational problems, so he wanted to disappoint everyone and give them the following problem.
You are given a tree consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. You need to add the minimum number of vertices to the tree such that the following conditions are satisfied after all additions:
Two paths are considered different if there exists at least one vertex which belongs to exactly one of the two paths.
Print the minimum number of vertices you have to add, then print the edges you added.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(1 \le n \le 2 \cdot 10^5, 1 \le k \le 10^6)$$$ — the number of vertices in the tree, and the desired length of the longest paths.
Each of the following $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le n, u \neq v)$$$ — the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.
Print a single integer $$$m$$$, representing the minimum number of vertices you have to add.
For each of the next $$$m$$$ lines, print two integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le n + m)$$$ — representing an edge you added to the tree.
3 31 22 3
3 2 4 2 5 1 6
Given an array of integers $$$a$$$ of length $$$n$$$. Let's say that segment $$$[l,r]$$$ is $$$i$$$-$$$th \: good$$$ if:
For each $$$i$$$ from $$$1$$$ to $$$n$$$, count all segments which are $$$i$$$-$$$th \: good$$$.
Here $$$\&$$$ denotes the bitwise AND operation.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$ — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output $$$n$$$ integers on a single line — the number of $$$i$$$-$$$th \: good$$$ segments for each $$$i$$$ $$$(1 \le i \le n)$$$.
2 5 3 7 6 2 10 5 4 1 3 1 4
2 1 2 8 1 1 3 1 3 1
For the first test case, $$$a=[3,7,6,2,10]$$$.
Mouf, a little penguin from Aleppo, dreamed of sunny beaches and coconuts. One day, his penguin companions created a challenge for him to unravel, with a ticket to their island awaiting as the reward.
Mouf was given an array of integers $$$a$$$ of length $$$n$$$. Mouf's companions consider a pair of indices $$$\langle i, j \rangle$$$ $$$(1 \le i \lt j \le n)$$$ $$$\it{coconut}$$$ if there exists positive integers $$$x$$$ and $$$y$$$ such that:
Help Mouf find the number of $$$\it{coconut}$$$ pairs in the array $$$a$$$ to win a trip to the magical island and enjoy sunny days and coconut feasts with his companions.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 5 \cdot 10^4)$$$ — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^4$$$.
For each test case, output a single integer — the number of $$$\it{coconut}$$$ pairs in the array $$$a$$$.
3 2 1 1 5 2 3 2 3 2 8 2 2 2 2 4 4 8 16
1 4 11
Fouad has just moved to a new city that contains $$$n$$$ houses and $$$m$$$ schools. In this problem, the city is represented as an infinite $$$2D$$$ plane, with both the schools and the houses represented as lattice points$$$^\dagger$$$ on this plane. Being a lazy person, Fouad wants to choose exactly one house and exactly one school in such a way that minimizes the Manhattan distance$$$^\ddagger$$$ between them. Please help him calculate the minimum Manhattan distance between any of the $$$n$$$ houses and any of the $$$m$$$ schools.
$$$^\dagger$$$ A lattice point is a point $$$p = (x, y)$$$ with integer coordinates $$$x$$$ and $$$y$$$.
$$$^\ddagger$$$ The Manhattan distance between two points $$$p_1 = (x_1, y_1)$$$ and $$$p_2 = (x_2, y_2)$$$ is equal to: $$$$$$d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|$$$$$$
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^3)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 2 \cdot 10^5)$$$ — the number of houses and schools respectively.
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ $$$(1 \le a_i, b_i \le 10^{18})$$$ — the coordinates of the $$$i$$$-th house.
Rhe $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$c_i$$$ and $$$d_i$$$ $$$(1 \le c_i, d_i \le 10^{18})$$$ — the coordinates of the $$$i$$$-th school.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the minimum Manhattan distance between any of the $$$n$$$ houses and any of the $$$m$$$ schools.
13 24 41 13 42 21 5
2
Explanation of the example:
The above figure illustrates the first test case in the example, where $$$H_i$$$ denotes the $$$i$$$-th house and $$$S_i$$$ denotes the $$$i$$$-th school. There are $$$6$$$ possible pairs for Fouad to choose from:
Therefore, the answer is $$$\min(\{4, 4, 2, 4, 3, 3\}) = 2$$$.
After a long ACPC contest, Mouf could finally buy a new Yu-Gi-Oh structure deck "Fire Kings". He was amazed by how strong this deck is, and now he is ready to duel with Jack Atlas, The Crimson King.
However, during the duel, Mouf found a little problem. Calculating the damage he inflicted on Jack is very complicated as Fire Kings monsters are burning the field! Mouf has $$$n$$$ monsters on the field, and each of them has the following five parameters:
Level $$$l$$$, strength $$$s$$$, developing rate $$$x$$$, damage suppression $$$ds$$$, and $$$t$$$, the number of turns it will remain on the field before it will be destroyed.
Every monster will do the following until the end of the $$$t_{th}$$$ turn, respectively:
It took Mouf a few minutes to calculate damage fast, and now he is challenging you -yes, you bot! For every monster given in the input, output the total damage it causes during all $$$t$$$ turns modulo $$$10^9+7$$$.
The first line of the input contains one integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^4)$$$ — the number of monsters Mouf has on the field.
Then next $$$n$$$ lines follows, each of them contains five integers $$$l$$$, $$$s$$$, $$$x$$$, $$$ds$$$, $$$t$$$ $$$(1 \le l, s, x, ds, t \le 10^9)$$$.
Print $$$n$$$ lines, the $$$i$$$-th line contains one integer, the total damage the $$$i$$$-th monster causes to Jack Atlas, modulo $$$10^9+7$$$.
11 8 2 2 3
90
35 4 6 9 82 9 5 8 36 9 8 1 9
149564498 656250204 11070
In the first test case, Mouf has a monster with these parameters: $$$l=1, s=8, x=2, ds=2, t=3$$$. Then the following will happen:
During the end of the third turn, the monster will be destroyed, and the total damage it causes is $$$8 + 4 + 2 + 24 + 12 + 40 = 90$$$.
For an array $$$a$$$, let's define the function $$$f(a)$$$ as the sum of the bitwise XOR sum of each non-empty subsequence$$$^\dagger$$$ of it.
For example, if $$$a$$$ = $$$[5 , 2 , 4]$$$, then $$$f(a) = (5 \oplus 2 \oplus 4) + (5 \oplus 2) + (5 \oplus 4) + (2 \oplus 4) + 5 + 2 + 4$$$.
Let's define an array of arrays $$$A$$$, which contains all non-empty subsequences$$$^\dagger$$$ of the array $$$a$$$.
You are given an array of $$$a$$$ of length $$$n$$$. You have to process the following $$$q$$$ updates:
Since this value could be too large, find it modulo $$$10^9 + 7$$$.
Here $$$\oplus$$$ denotes the bitwise XOR operation.
$$$\dagger$$$ A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.
The first line contains one integer $$$n$$$ $$$(1 \le n \le 10^6)$$$ — the length of the array $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the elements of the array $$$a$$$.
The third line contains one integer $$$q$$$ $$$(1 \le q \le 10^6)$$$ — the number of update operations.
Each of the next $$$q$$$ lines contains two integers $$$i$$$ and $$$x$$$ $$$(1 \le i \le n, 1 \le x \le 10^9)$$$ — denoting the updated index and value.
After each update, output a single integer — the sum $$$\displaystyle\sum_{s \in A} f(s)$$$, modulo $$$10^9 + 7$$$.
31 2 331 22 52 7
35 72 74
For the first update, $$$A = [[2],[2],[3],[2,2],[2,3],[2,3],[2,2,3]]$$$, then:
$$$f([2]) + f([2]) + f([3]) + f([2,2]) + f([2,3]) + f([2,3]) + f([2,2,3]) = 2 + 2 + 3 + 4 + 6 + 6 + 12 = 35$$$.
Besher and his wife are playing a game on $$$n$$$ piles of stones, each one contains $$$a_i$$$ stones. On each player's turn, they can do one of these actions:
The first player who is unable to make a move loses (all piles are empty).
Given that Besher goes first, who will win the game if both players play optimally?
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 2 \cdot 10^{5})$$$ — the number of piles in the game.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the elements of the array $$$a$$$, describing the initial number of stones.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output "YES" if Besher wins, and output "NO" otherwise.
331 2 321 211
NO YES YES
Our lovely judge Fofo loves "bitset" very much, and when the judges were discussing the problems to be put in this contest, he sometimes heard the word "bitset" being mentioned.
As he loves "bitset" very much, he usually says his favorite word "7as" when the solution includes "bitset" as a substring$$$^\dagger$$$, and when he does not find "bitset", he gets sad and says "no 7as for you today".
Fofo wants to automate this procedure, so can you help him do that?
$$$\dagger$$$ A string $$$t$$$ is a substring of another string $$$s$$$ if you can obtain $$$t$$$ from $$$s$$$ by deleting (several, maybe $$$0$$$) characters from the beginning of $$$s$$$ and deleting (several, maybe $$$0$$$) characters from the end of $$$s$$$.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. The description of the test cases follows.
The only line of each test case contains a single string of lowercase English letters $$$s$$$ ($$$1 \le |s| \le 26$$$) — the solution to the problem.
For each test case, print a single line — "7as" if the string contains "bitset", and "no 7as for you today" otherwise (without quotation marks).
6fofolovesbitsetobitsetobitsetobitnosetblueblisteringbarnaclesthearkoffzbitset
7as 7as no 7as for you today no 7as for you today no 7as for you today 7as
In this problem, you are given $$$2$$$ points describing a square (i.e. the upper right and lower left corners) such that the sides of the square are parallel to either $$$x$$$-axis and $$$y$$$-axis.
You are also given $$$n$$$ segments such that each segment is strictly inside the square and parallel to one of the axes.
You need to find whether there exist two segments $$$i$$$ and $$$j$$$ ($$$i \neq j$$$) such that if both were extended to a line, they divide the square into multiple rectangles, where only two of them are equal$$$^\dagger$$$.
$$$^\dagger$$$ Two rectangles are considered equal if their sides are equal apart from the vertical or horizontal alignment.
The first line contains five integers $$$n,x_s,y_s,x_e,y_e$$$ $$$(1 \le n \le 10^5, 1 \le x_s,y_s,x_e,y_e \le 10^9)$$$ — the number of segments, and the coordinates of the lower left and upper right corners of the square.
Each of the next $$$n$$$ lines contains four integers $$$x_1, y_1, x_2, y_2$$$ $$$(1 \le x_1, y_1, x_2, y_2 \le 10^9)$$$ — the coordinates of the endpoints of the segments.
It is guaranteed that all the segments are parallel to one of the coordinate axes, and strictly lie inside the square. Segments may touch, overlap, and even completely coincide.
On a single line, print the two indices of segments you chose. If there are multiple answers, you may print any of them. Otherwise, if there is no answer, print "$$$-1$$$ $$$-1$$$" (without quotation marks).
6 1 1 10 10 9 8 8 8 9 5 3 5 5 2 5 8 4 4 7 4 7 3 7 5 6 2 6 9
4 5
There are $$$n$$$ water tanks, each one liter in size. Water tanks are connected by pipes, where a pipe connects tank $$$v$$$ to parent tank $$$p_v$$$. Tank $$$1$$$ is the root.
Let's define the process of filling water tank $$$v$$$ with $$$x$$$ liters as follows:
Here are some examples of the process described above:
![]() | ![]() |
| filling tank 5 with 2 liters | filling tank 6 with 4 liters |
Given $$$q$$$ queries of the form $$$(v, x, u)$$$, you should answer the following task:
If $$$x$$$ liters are added to tank $$$v$$$, will tank $$$u$$$ be filled as well?
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ $$$(2 \le n, q \le 10^5)$$$ — the number of water tanks and queries respectively.
The second line of each test case contains $$$n - 1$$$ integers $$$p_2, \ldots, p_n$$$ $$$(1 \le p_i \le n)$$$ — the parent of tank $$$i$$$, i.e., there is a pipe between $$$i$$$ and $$$p_i$$$ in the tree.
Each of the next $$$q$$$ lines contains three integers $$$v_i, x_i, u_i$$$ $$$(1 \le v_i, x_i, u_i \leq n)$$$ — indicating the $$$i$$$-th query.
It is guaranteed that the given set of pipes forms a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$.
For each query, output "YES" if the tank will be filled, and output "NO" otherwise.
2 6 6 1 1 2 2 3 5 2 2 5 2 1 5 2 5 6 4 3 6 4 2 6 4 4 7 7 1 1 2 2 3 3 3 2 7 3 3 7 3 2 1 3 4 1 3 3 1 3 6 4 2 3 4
YES NO YES YES YES NO NO YES NO YES NO YES YES
Given an integer $$$n \ge 3$$$, determine if there exists a non-self-intersecting$$$^\dagger$$$ polygon with $$$n$$$ distinct vertices of integer coordinates such that:
In case such a polygon exists, provide a construction.
$$$^\dagger$$$ A polygon with $$$n$$$ distinct vertices is non-self-intersecting if no two distinct sides intersect except in the vertices.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$ — the number of test cases. The description of the test cases follows.
The first and only line of each test case contains a single integer $$$n$$$ $$$(3 \le n \le 10^4)$$$ — the number of vertices.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case:
If there are multiple possible answers, you may print any of them.
2 3 4
-1 0 0 1 0 1 1 0 1
The polygon is non-self-intersecting but sides are not equal.
There exist a pair of three consecutive vertices $$$[6, 1, 2]$$$ and $$$[2, 3, 4]$$$ which are collinear.
The polygon is self-intersecting, as there exist two distinct sides $$$[3, 4]$$$ and $$$[5, 6]$$$ that intersect in a non-vertex point.
Given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ where $$$|a_i| = 1$$$ for each index $$$i$$$ $$$(1 \le i \le n)$$$, and two integers $$$l$$$ and $$$r$$$ $$$(l \le r)$$$.
We have a real-valued function $$$f$$$ defined as follows:
$$$$$$f(x) = \sum_{i=1}^{n} |a_i \cdot x + b_i|.$$$$$$
Suppose we randomly pick a real value $$$m$$$ in the range $$$(l \le m \le r)$$$. What is the probability that $$$f(m)$$$ equals the minimum value of $$$f$$$ in the given range $$$[l, r]$$$. More formally,
$$$$$$f(m) = \displaystyle\min_{l \le x \le r} f(x).$$$$$$
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers $$$n, l, r$$$ $$$(1 \le n \le 2 \cdot 10^5, -10^9 \le l \le r \le 10^9)$$$ — the length of the array $$$a$$$, and the endpoints of the given range.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(|a_i| = 1)$$$ — the elements of the array $$$a$$$.
The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ $$$(-10^9 \le b_i \le 10^9)$$$ — the elements of the array $$$b$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output the probability that $$$f(m)$$$ equals the minimum value of $$$f$$$ in the given range $$$[l, r]$$$. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$.
Formally, let your answer be $$$p$$$, and the jury's answer be $$$q$$$. Your answer is considered correct if and only if $$$\dfrac{|p - q|}{\max(1, |q|)} \le 10^{-9}$$$.
3 1 2 2 -1 3 1 2 4 -1 3 2 3 5 1 1 -2 -4
1.0000000000 0.0000000000 0.5000000000
For the first test case, there is only one possible value for $$$m$$$ which satisfies $$$f(m) = \displaystyle\min_{2 \le x \le 2} f(x) = 1$$$, and it is $$$2$$$. Thus, the probability of randomly picking $$$2$$$ in the range $$$[2, 2]$$$ is $$$1$$$.
For the second test case, there is only one possible value for $$$m$$$ which satisfies $$$f(m) = \displaystyle\min_{2 \le x \le 4} f(x) = 0$$$, and it is $$$3$$$. Thus, the probability of randomly picking $$$3$$$ in the range $$$[2, 4]$$$ is $$$0$$$.
For the third test case, there is a range of possible values for $$$m$$$ which satisfies $$$f(m) = \displaystyle\min_{3 \le x \le 5} f(x) = 2$$$, and it is $$$[3, 4]$$$. Thus, if we randomly pick a value in the range $$$[3, 5]$$$, the probability that it lies in the range $$$[3, 4]$$$ is $$$\dfrac{1}{2} = 0.5$$$.
Given an integer $$$x$$$. Find the smallest integer $$$y$$$ that satisfy the following:
Here, $$$string(x)$$$ is the decimal representation of $$$x$$$ (without leading zeros) as a string.
Strings are compared lexicographically$$$^\dagger$$$. For example,
If such an integer $$$y$$$ does not exist, print $$$-1$$$.
$$$^\dagger$$$ A string $$$s$$$ is lexicographically larger than a string $$$t$$$ if and only if one of the following holds:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^3)$$$ — the number of test cases. The description of the test cases follows.
The first and only line of each test case contains a single integer $$$x$$$ $$$(1 \le x \le 10^{18})$$$.
For each test case:
1 9
10