inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Note the unusual definition of $\text{MEX}$ in this problem.

Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$.

If a set of paths $P = \\{(x_i, y_i)\\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more.

Turtle defines the value of a set of paths as $\sum\limits_{(x, y) \in P} f(x, y)$, where $f(x, y)$ denotes the $\text{MEX}^{\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$).

Turtle wonders the minimum value over all good sets of paths. Please help him calculate the answer!

$^{\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons.

$^{\ddagger}\text{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\text{MEX}$ of $[2, 3]$ is $1$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 2.5 \cdot 10^4$) — the number of vertices in the tree.

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 sequence $a$.

The third line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$) — the parent of each vertex in the tree.

Additional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single integer — the minimum value over all good sets of paths.

Tags:
['data structures', 'dp', 'trees']

Time Limit: 4000 ms
Memory Limit: 1024 MB
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $x$ : $(y + 1)$.

While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score?

It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence.

Help Dima and answer the question!

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \le x_{1}, y_{1} \le 10^{9}$, $x_{1} \neq y_{1}$) — the score before Dima was distracted.

The second line of each test case contains two integers $x_{2}, y_{2}$ ($x_{1} \le x_{2} \le 10^{9}$, $y_{1} \le y_{2} \le 10^{9}$, $x_{2} \neq y_{2}$) — the score when Dima returned.

Output Format:
For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes.

You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['greedy', 'implementation', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture:

There is a variable $x$ and a constant $y$. The following operation is performed $k$ times:

  * increase $x$ by $1$, then    * while the number $x$ is divisible by $y$, divide it by $y$. 

Note that both of these actions are performed sequentially within one operation.

For example, if the number $x = 16$, $y = 3$, and $k = 2$, then after one operation $x$ becomes $17$, and after another operation $x$ becomes $2$, because after adding one, $x = 18$ is divisible by $3$ twice.

Given the initial values of $x$, $y$, and $k$, Maxim wants to know what is the final value of $x$.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. Then follows the description of the test cases.

The only line of each test case contains three integers $x$, $y$, and $k$ ($1 \le x, k \le 10^{9}$, $2 \le y \le 10^{9}$) — the initial variable, constant and the number of operations.

Output Format:
For each test case, output a single integer — the number obtained after applying $k$ operations.

Tags:
['brute force', 'implementation', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB
On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play.

Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a non-zero number of cards from the top of the deck and finishes the round. If the sum of the numbers on the cards collected during the round is between $l$ and $r$, inclusive, the round is won; otherwise, it is lost.

Egor knows by heart the order of the cards. Help Egor determine the maximum number of rounds he can win in such a game. Note that Egor is not required to win rounds consecutively.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains three integers $n$, $l$, and $r$ ($1 \le n \le 10^{5}$, $1 \le l \le r \le 10^9$).

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 numbers on the cards from top to bottom.

It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^{5}$.

Output Format:
For each test case, output a single number — the maximum number of rounds Egor can win.

Tags:
['binary search', 'data structures', 'dp', 'greedy', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
Nikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of $n$ rows and $m$ columns, with each cell containing a non-negative integer representing the height of the mountain.

He also noticed that mountains come in two types:

  * With snowy caps.    * Without snowy caps. 

Nikita is a very pragmatic person. He wants the sum of the heights of the mountains with snowy caps to be equal to the sum of the heights of the mountains without them. He has arranged with the mayor of Berlyand, Polikarp Polikarpovich, to allow him to transform the landscape.

Nikita can perform transformations on submatrices of size $k \times k$ as follows: he can add an integer constant $c$ to the heights of the mountains within this area, but the type of the mountain remains unchanged. Nikita can choose the constant $c$ independently for each transformation. Note that $c$ can be negative.

Before making the transformations, Nikita asks you to find out if it is possible to achieve equality of the sums, or if it is impossible. It doesn't matter at what cost, even if the mountains turn into canyons and have negative heights.

If only one type of mountain is represented on the map, then the sum of the heights of the other type of mountain is considered to be zero.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. This is followed by a description of test cases.

The first line of each test case contains three integers $n, m, k$ ($1 \le n, m \le 500, 1 \le k \le min(n, m)$).

The next $n$ lines of each test case contain $m$ integers $a_{i j}$ ($0 \le a_{i j} \le 10^{9}$) — the initial heights of the mountains.

The next $n$ binary strings of length $m$ for each test case determine the type of mountain, '$0$' — with snowy caps, '$1$' — without them.

It is guaranteed that the sum of $n \cdot m$ for all test cases does not exceed $250\,000$.

Output Format:
For each test case, output "YES" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['brute force', 'data structures', 'implementation', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$.

A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \le i \le r$ condition $bit(a_{i}) \le k$ is satisfied.

You are given an array $a$ of length $n$, consisting of consecutive non-negative integers starting from $0$, i.e., $a_{i} = i$ for $0 \le i \le n - 1$ (in $0$-based indexing). You need to count the number of $k$-good subarrays in this array.

As the answer can be very large, output it modulo $10^{9} + 7$.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) — the number of test cases. The following lines describe the test cases.

The single line of each test case contains two integers $n$, $k$ ($1 \le n \le 10^{18}, 1 \le k \le 60$).

Output Format:
For each test case, output a single integer — the number of $k$-good subarrays modulo $10^{9} + 7$.

Tags:
['bitmasks', 'brute force', 'combinatorics', 'divide and conquer', 'dp', 'math', 'meet-in-the-middle']

Time Limit: 2000 ms
Memory Limit: 256 MB
You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:

What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted in non-decreasing order?

More formally, you want to select a subarray of the array $(l, r)$ with the minimum value of $r - l + 1$. After that, you will sort the elements $a_{l}, a_{l + 1}, \ldots, a_{r}$ and want the condition $a_{i} \le a_{i + 1}$ to hold for all $1 \le i < n$. If the array is already sorted in non-decreasing order, then $l$ and $r$ should be considered as equal to $-1$.

Note that finding such $(l, r)$ does not change the array in any way. The modifications themselves take the form: assign $a_{pos} = x$ for given $pos$ and $x$.

Input Format:
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) — the number of test cases. Then follows the description of test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^{5}$).

The second line of each test case contains $n$ integers $a_{i}$ ($0 \le |a_{i}| \le 10^{9}$) — the initial elements of the array $a$.

The third line of each test case contains a number $q$ ($0 \le q \le 5 \cdot 10^{5}$) — the number of modifications to the array.

The following $q$ lines of each test case contain two integers $pos_{i}$ ($1 \le pos_{i} \le n$) and $val_{i}$ ($0 \le |val_{i}| \le 10^{9}$) — this means that for the $i$-th modification, $a_{pos_{i}}$ is assigned the value $val_{i}$.

It is guaranteed that the sum of $n$ and the sum of $q$ for all test cases does not exceed $5 \cdot 10^{5}$.

Output Format:
For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ — the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.

Tags:
['binary search', 'data structures', 'sortings']

Time Limit: 2500 ms
Memory Limit: 256 MB
An array of integers $a_1,a_2,\cdots,a_n$ is beautiful subject to an integer $k$ if it satisfies the following:

  * The sum of $a_{j}$ over all $j$ such that $j$ is a multiple of $k$ and $1 \le j \le n $, itself, is a multiple of $k$.    * More formally, if $\sum_{k | j} a_{j}$ is divisible by $k$ for all $1 \le j \le n$ then the array $a$ is beautiful subject to $k$. Here, the notation ${k|j}$ means $k$ divides $j$, that is, $j$ is a multiple of $k$. 

Given $n$, find an array of positive nonzero integers, with each element less than or equal to $10^5$ that is beautiful subject to all $1 \le k \le n$.

It can be shown that an answer always exists.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

The first and only line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array.

Output Format:
For each test case, print the required array as described in the problem statement.

Tags:
['constructive algorithms', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given two grids of numbers $a$ and $b$, with $n$ rows and $m$ columns. All the values in the grid are $0$, $1$ or $2$.

You can perform the following operation on $a$ any number of times:

  * Pick any subrectangle in the grid with length and width $\ge 2$. You are allowed to choose the entire grid as a subrectangle.    * The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $1$ to their values modulo $3$.    * For the pair of corners not picked, add $2$ to their values modulo $3$. 

Note that the operation only changes the values of the corners of the picked subrectangle.

Is it possible to convert the grid $a$ into grid $b$ by applying the above operation any number of times (possibly zero)?

Input Format:
The first line contains an integer $t$, the number of testcases ($1 \le t \le 250$).

For each testcase:

The first line contains two integers $n$ and $m$, the number of rows and columns in the grid ($2 \le n,m \le 500$).

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $a_{i,j}$.

Each of the next n lines contain m characters — the $j$-th character of the $i$-th line represents $b_{i,j}$ ($0 \le a_{i,j}, b_{i,j} \le 2$).

It is guaranteed that the sum of $n$ over all test cases and the sum of $m$ over all test cases do not exceed $500$.

Output Format:
For each test case print "YES" (without quotes) if it is possible to convert grid $a$ into grid $b$ and "NO" (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Tags:
['constructive algorithms', 'greedy', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Alice, Bob and Charlie want to share a rectangular cake cut into $n$ pieces. Each person considers every piece to be worth a different value. The $i$-th piece is considered to be of value $a_i$ by Alice, $b_i$ by Bob and $c_i$ by Charlie.

The sum over all $a_i$, all $b_i$ and all $c_i$ individually is the same, equal to $tot$.

Given the values of each piece of the cake for each person, you need to give each person a contiguous slice of cake. In other words, the indices at the left and right ends of these subarrays (the slices given to each person) can be represented as $(l_a, r_a)$, $(l_b, r_b)$ and $(l_c, r_c)$ respectively for Alice, Bob and Charlie. The division needs to satisfy the following constraints:

  * No piece is assigned to more than one person, i.e., no two subarrays among $[l_a,\ldots,r_a]$, $[l_b, \ldots, r_b]$ and $[l_c, \ldots, r_c]$ intersect.   * $ \sum_{i = l_a}^{r_a} a_i, \sum_{i = l_b}^{r_b} b_i, \sum_{i = l_c}^{r_c} c_i \geq \lceil \frac{tot}{3} \rceil$. 

Here, the notation $\lceil \frac{a}{b} \rceil$ represents ceiling division. It is defined as the smallest integer greater than or equal to the exact division of $a$ by $b$. In other words, it rounds up the division result to the nearest integer. For instance $\lceil \frac{10}{3} \rceil = 4$ and $\lceil \frac{15}{3} \rceil = 5$.

Input Format:
The first line contains an integer $t$, the number of testcases, ($1 \le t \le 10^4$)

For each testcase:

The first line contains the integer $n$ ($3 \le n \le 2 \cdot 10^5$).

The following three lines contain $n$ integers each:

One line with $n$ integers $a_1, a_2, \ldots, a_n$ represents the values for Alice ($1 \le a_i \le 10^6$).

The next line with $n$ integers $b_1, b_2, \ldots, b_n$ represents the values for Bob ($1 \le b_i \le 10^6$).

The next line with $n$ integers $c_1, c_2, \ldots, c_n$ represents the values for Charlie ($1 \le c_i \le 10^6$).

It is guaranteed that $ \sum_{i = 1}^{n} a_i = \sum_{i = 1}^{n} b_i = \sum_{i = 1}^{n} c_i$.

The sum of $n$ over all testcases does not exceed $2 \cdot 10^5$.

Output Format:
For each testcase, output $-1$ if the required condition is impossible.

Otherwise, output six numbers – $l_a, r_a, l_b, r_b, l_c, r_c$, the respective starting and ending indices ($1$-indexed) of the subarrays for Alice, Bob and Charlie, respectively.

Tags:
['binary search', 'brute force', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
Given two arrays of distinct positive integers $a$ and $b$ of length $n$, we would like to make both the arrays the same. Two arrays $x$ and $y$ of length $k$ are said to be the same when for all $1 \le i \le k$, $x_i = y_i$.

Now in one move, you can choose some index $l$ and $r$ in $a$ ($l \le r$) and swap $a_l$ and $a_r$, then choose some $p$ and $q$ ($p \le q$) in $b$ such that $r-l=q-p$ and swap $b_p$ and $b_q$.

Is it possible to make both arrays the same?

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2 \cdot 10^4$). 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 arrays $a$ and $b$.

The second line of each test case contains $n$ distinct integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — the integers in the array $a$.

The third line of each test case contains $n$ distinct integers $b_1,b_2,b_3,\ldots,b_n$ ($1 \le b_i \le 2 \cdot 10^5$) — the integers in the array $b$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
For each testcase, print "YES" if the arrays $a$ and $b$ can be made the same. Otherwise, print "NO". can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Tags:
['constructive algorithms', 'data structures', 'divide and conquer', 'greedy', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it.

The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $0$ at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn.

They play this game until no balls are remaining in the game. Alice plays first.

Find the expected score that both the players have at the end of the game modulo $10^9+7$.

Formally, let $M = 10^9+7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

Input Format:
There are multiple test cases. The first line of the input contains an integer $t$, the number of test cases ($1 \le t \le 2 \cdot 10^5$).

Each test case description is on a new line. The first line of the test case contains two integers $n$ and $k$ in the respective order separated by a space ($1 \le k \le n \le 4 \cdot 10^5$).

The second line of the test case contains $n$ integers: $v_1, v_2, \ldots, v_n$, the value for each ball separated by spaces. The first $k$ balls are special ($1 \le v_i \le 10^7$).

The sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $10^9+7$.

Tags:
['combinatorics', 'math', 'probabilities']

Time Limit: 2000 ms
Memory Limit: 256 MB
You have an array of non-negative integers $a_1, a_2, \ldots, a_n$.

The value of a sub-array of length $\ge 2$, $a[l, r] = [a_l, a_{l+1}, \ldots, a_r]$ is the minimum value of $a_i \oplus a_j$ such that $l \le i < j \le r$, where $\oplus$ is the xor (exclusive-or) operator.

You have to find the $k$-th smallest value over all sub-arrays of length $\ge 2$.

Input Format:
The first line of the input contains multiple test cases $t$ ($1 \le t \le 2 \cdot 10^4$).

The first line of each test case contains integer numbers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le \frac{n\cdot(n-1)}{2}$).

The second line of the input contains $n$ non-negative integer numbers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the array itself.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
Print the $k$-th smallest value obtained over all subarrays of length at least $2$.

Tags:
['binary search', 'bitmasks', 'data structures', 'greedy', 'two pointers']

Time Limit: 4000 ms
Memory Limit: 256 MB