inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
This is the easy version of the problem. In this version, it is guaranteed that $q = 0$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). 

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

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

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k \le 10^5$; $q = 0$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output Format:
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Tags:
['2-sat', 'binary search', 'combinatorics', 'constructive algorithms', 'dfs and similar', 'dsu', 'graphs']

Time Limit: 2000 ms
Memory Limit: 512 MB
This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). 

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

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

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output Format:
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Tags:
['binary search', 'combinatorics', 'data structures', 'dsu', 'graphs']

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.

More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.

$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.

$^{\ddagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input Format:
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 number of arrays.

Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.

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

Output Format:
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).

Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:

  * They can submit the problem and receive $a_i$ points;    * They can skip the problem, in which case they will never be able to submit it. 

Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:

  * If he submitted the $i$-th problem, it looks at problems with indices $j < i$;    * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$. 

Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.

Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — 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 4 \cdot 10^5$) — the number of problems in the olympiad.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the scores of the problems.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) — the parameters of the problems.

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

Output Format:
For each test case, output a single integer — the maximum number of points that Prokhor can achieve.

Tags:
['binary search', 'dp', 'graphs', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB
Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity.

The conditions in this casino are as follows:

  * There are a total of $n$ games in the casino.    * You can play each game at most once.    * Each game is characterized by two parameters: $p_i$ ($1 \le p_i \le 100$) and $w_i$ — the probability of winning the game in percentage and the winnings for a win.    * If you lose in any game you decide to play, you will receive nothing at all (even for the games you won). 

You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings.

In this case, if you choose to play the games with indices $i_1 < i_2 < \ldots < i_k$, you will win in all of them with a probability of $\prod\limits_{j=1}^k \frac{p_{i_j}}{100}$, and in that case, your winnings will be equal to $\sum\limits_{j=1}^k w_{i_j}$.

That is, the expected value of your winnings will be $\left(\prod\limits_{j=1}^k \frac{p_{i_j}}{100}\right) \cdot \left(\sum\limits_{j=1}^k w_{i_j}\right)$.

To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $i$ ($1 \le i \le n$), it holds that $w_i \cdot p_i \le 2 \cdot 10^5$.

Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of games offered to play.

The $i$-th of the following $n$ lines contains two integers $p_i$ and $w_i$ ($1 \leq p_i \leq 100$, $1 \leq w_i, p_i \cdot w_i \leq 2 \cdot 10^5$) — the probability of winning and the size of the winnings in the $i$-th game.

Output Format:
Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games.

Your answer will be accepted if the relative or absolute error does not exceed $10^{-6}$. Formally, if $a$ is your answer and $b$ is the jury's answer, it will be accepted if $\frac{|a-b|}{\max(b, 1)} \le 10^{-6}$.

Tags:
['brute force', 'dp', 'greedy', 'math', 'probabilities']

Time Limit: 2000 ms
Memory Limit: 256 MB
In the heart of an ancient kingdom grows the legendary Tree of Life — the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ channels in the tree, with the $i$-th channel connecting nodes $v_i$ and $u_i$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree.

However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $v_1, v_2, \ldots, v_k$, where each pair of adjacent nodes $v_i$ and $v_{i+1}$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $(v_i, v_{i+1})$ and $(v_{i+1}, v_{i+2})$ is blocked for each $1 \leq i \leq k - 2$.

The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.

Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 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$ ($2 \leq n \leq 5 \cdot 10^5$) — the number of nodes in the Tree of Life.

The $i$-th of the following $n - 1$ lines of each test case contains two integers $v_i$ and $u_i$ ($1 \leq v_i < u_i \leq n$) — the channel connecting nodes $v_i$ and $u_i$.

It is guaranteed that there exists a unique simple path through the channels between any two nodes.

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

Output Format:
For each test case, output a single integer — the minimum number of paths that the sages need to select to prevent a catastrophe.

Tags:
['dp', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB
In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck. The road is divided into $n$ sections, numbered from $1$ to $n$ from left to right. The height of the surface in the $i$-th section is equal to $a_i$. If the height of the $i$-th section is greater than $0$, then the dump truck must take sand from the $i$-th section of the road, and if the height of the $i$-th section is less than $0$, the dump truck must fill the pit in the $i$-th section of the road with sand. It is guaranteed that the initial heights are not equal to $0$.

When the dump truck is in the $i$-th section of the road, it can either take away $x$ units of sand, in which case the height of the surface in the $i$-th section will decrease by $x$, or it can fill in $x$ units of sand (provided that it currently has at least $x$ units of sand in its bed), in which case the height of the surface in the $i$-th section of the road will increase by $x$.

The dump truck can start its journey from any section of the road. Moving to an adjacent section on the left or right takes $1$ minute, and the time for loading and unloading sand can be neglected. The dump truck has an infinite capacity and is initially empty.

You need to find the minimum time required for the dump truck to level the sand so that the height in each section becomes equal to $0$. Note that after all movements, the dump truck may still have sand left in its bed. You need to solve this problem independently for the segments numbered from $l_i$ to $r_i$. Sand outside the segment cannot be used.

Input Format:
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$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of sections and the number of queries.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$, $a_i \neq 0$) — the initial height in each section.

The $i$-th of the following $q$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the boundaries of the segment of sections for which the minimum time needs to be determined.

It is guaranteed that the sum of $n$ over all test cases and the sum of $q$ over all test cases do not exceed $3 \cdot 10^5$.

Output Format:
For each query, output the minimum time required to level the sand in the segment $[l_i, r_i]$, or $-1$ if it is impossible.

Tags:
['data structures', 'greedy', 'math', 'matrices']

Time Limit: 3000 ms
Memory Limit: 512 MB
Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins.

There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum amount required to open the "Profitable" deposit decreases by $2x$ coins. However, these coins cannot later be deposited into the "Profitable" deposit.

Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $0$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.

Input Format:
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.

A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) — the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit.

Output Format:
For each test case, output a single integer — the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.

Tags:
['greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot.

When you press the button corresponding to the $i$-th slot, one of two events occurs:

  * If there is a can of lemonade in the $i$-th slot, it will drop out and you will take it. At this point, the number of cans in the $i$-th slot decreases by $1$.   * If there are no cans of lemonade left in the $i$-th slot, nothing will drop out. 

After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: $a_1, a_2, \ldots, a_n$.

Determine the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.

Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least $k$ cans of lemonade in total in the machine. In other words, $k \leq a_1 + a_2 + \ldots + a_n$.

Input Format:
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 $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) — the number of slots in the machine and the required number of cans of lemonade.

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 number of cans in the slots.

It is guaranteed that $k \leq a_1 + a_2 + \ldots + a_n$, meaning there are at least $k$ cans of lemonade in the machine.

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

Output Format:
For each test case, output a single integer — the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.

Tags:
['binary search', 'constructive algorithms', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.

More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.

$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.

$^{\ddagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input Format:
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 number of arrays.

Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.

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

Output Format:
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.

Tags:
['constructive algorithms', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB