inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.

Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.

Define the operation $\mathrm{pop}$ as follows:

  1. initialize variable $v$ as $1$;    2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$);      1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them;      2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$);      3. assign $x$ to $v$ (i.e. $v := x$);    3. assign $-1$ to $a_v$ (i.e. $a_v := -1$). 

Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.

A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.

A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first and the second time.

Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:

  * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$. 

Two heaps are considered different if there is a node which has different values in the heaps.

Since the answer might be large, print it modulo $p$.

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

The first line of each test case contains three integers $n, k, p$ ($2 \le n \le 100$, $1 \le k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).

It is guaranteed that the sum of $n$ does not exceed $100$ and the sum of $k$ over all test cases does not exceed $500$.

Output Format:
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB
You received an $n\times m$ grid from a mysterious source. The source also gave you a magic positive integer constant $k$.

The source told you to color the grid with some colors, satisfying the following condition:

  * If $(x_1,y_1)$, $(x_2,y_2)$ are two distinct cells with the same color, then $\max(|x_1-x_2|,|y_1-y_2|)\ge k$. 

You don't like using too many colors. Please find the minimum number of colors needed to color the grid.

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

The only line of each test case consists of three positive integers $n$, $m$, $k$ ($1\le n,m,k\le10^4$) — the dimensions of the grid and the magic constant.

Output Format:
For each test case, print a single integer — the minimum number of colors needed to color the grid.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays.

In each turn, the following events happen in order:

  * Alice chooses either the first or the last element of her array and removes it from the array;    * Bob chooses either the first or the last element of his array and removes it from the array. 

The game continues for $n-1$ turns, after which both arrays will have exactly one remaining element: $x$ in the array $a$ and $y$ in the array $b$.

If $x=y$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally.

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

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

The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$ are distinct) — the permutation of Alice.

The next line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_i\le n$, all $b_i$ are distinct) — the permutation of Bob.

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

Output Format:
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$.

The circles' radii increase at a rate of $1$ unit per second.

You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circle (including the moment you reach $(x_t,y_t)$). You can move in any direction you want. However, your speed is limited to $1$ unit per second.

Please determine whether this is possible.

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

The first line of each test case contains a single integer $n$ ($1\le n\le10^5$) — the number of circles.

The next $n$ lines each contain two integers $x_i$, $y_i$ ($1\le x_i,y_i\le10^9$) — the center of each circle.

The final line contains four integers $x_s$, $y_s$, $x_t$, $y_t$ ($1\le x_s,y_s,x_t,y_t\le10^9$) — the coordinates of the starting point and the goal, respectively.

It is guaranteed that these $n+2$ points are distinct.

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

Output Format:
For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, and output $\texttt{NO}$ otherwise.

You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as a positive response).

Tags:
['brute force', 'geometry', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved.

You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$.

You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\ddagger$ of the given tree.

Please note that the swaps are persistent through queries.

$^\dagger$ A perfect binary tree is a tree with vertex $1$ as its root, with size $n=2^k-1$ for a positive integer $k$, and where the parent of each vertex $i$ ($1<i\le n$) is $\left\lfloor\frac{i}{2}\right\rfloor$. Thus, all leaves of this tree are at a distance $k - 1$ from the root.

$^\ddagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree.

              dfs_order = []          function dfs(v):         append v to the back of dfs_order         pick an arbitrary permutation s of children of v         for child in s:             dfs(child)     dfs(1)     

Note that the DFS order is not unique.

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

The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) — the number of vertices in the tree and the number of queries. It is guaranteed that $n=2^k-1$ for a positive integer $k$.

The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) — the parent of each vertex in the given tree. It is guaranteed that $a_i=\left\lfloor\frac{i}{2}\right\rfloor$.

The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) — the initial permutation $p$.

The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) — the positions of the elements to swap in the permutation.

It is guaranteed that the sum of all $n$ does not exceed $65\,535$, and the sum of all $q$ does not exceed $5 \cdot 10^4$.

Output Format:
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise.

You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$ and $\texttt{YES}$ will be recognized as a positive response).

Tags:
['brute force', 'data structures', 'dfs and similar', 'graphs', 'hashing', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved.

You are given a rooted tree consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$.

You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\dagger$ of the given tree.

Please note that the swaps are persistent through queries.

$^\dagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree.

              dfs_order = []          function dfs(v):         append v to the back of dfs_order         pick an arbitrary permutation s of children of v         for child in s:             dfs(child)     dfs(1)     

Note that the DFS order is not unique.

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

The first line of each test case contains two integers $n$, $q$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) — the number of vertices in the tree and the number of queries.

The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) — the parent of each vertex in the given tree.

The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) — the initial permutation $p$.

The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) — the positions of the elements to swap in the permutation.

It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$, and the sum of all $q$ does not exceed $10^5$.

Output Format:
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise.

You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as a positive response).

Tags:
['binary search', 'data structures', 'dfs and similar', 'graphs', 'hashing', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB
Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$.

Define the strength of an array as the number of seconds it takes to become empty.

You are given an array of integers compressed in the form of $n$ pairs that describe the array left to right. Each pair $(a_i,b_i)$ represents $a_i$ copies of $b_i$, i.e. $\underbrace{b_i,b_i,\cdots,b_i}_{a_i\textrm{ times}}$.

For each $i=1,2,\dots,n$, please find the strength of the sequence described by the first $i$ pairs.

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

The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) — the length of sequence $a$.

The next $n$ lines contain two integers each $a_i$, $b_i$ ($1\le a_i\le10^9,0\le b_i\le n$) — the pairs which describe the sequence.

It is guaranteed that the sum of all $n$ does not exceed $3\cdot10^5$.

It is guaranteed that for all $1\le i<n$, $b_i\neq b_{i+1}$ holds.

Output Format:
For each test case, print one line containing $n$ integers — the answer for each prefix of pairs.

Tags:
['brute force', 'data structures', 'dp']

Time Limit: 2000 ms
Memory Limit: 512 MB
This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved.

In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.

Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:

  * after every round, $\gcd(W_L,W_F)\le 1$;    * at the end of the match, $W_L\le n, W_F\le m$. 

Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.

Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.

Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.

Input Format:
The first line contains an integer $t$ ($1\leq t \leq 10^3$) — the number of test cases.

The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.

Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.

Output Format:
For each test case, output a single integer — the maximum total score of a successful performance.

Tags:
['brute force', 'dfs and similar', 'dp', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 512 MB
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved.

In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.

Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:

  * after every round, $\gcd(W_L,W_F)\le 1$;    * at the end of the match, $W_L\le n, W_F\le m$. 

Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.

Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.

Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.

Input Format:
The first line contains an integer $t$ ($1\leq t \leq 10^3$) — the number of test cases.

The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$): $n$, $m$ give the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.

Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.

Output Format:
For each test case, output a single integer — the maximum total score of a successful performance.

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

Time Limit: 4000 ms
Memory Limit: 512 MB
Consider a grid graph with $n$ rows and $n$ columns. Let the cell in row $x$ and column $y$ be $(x,y)$. There exists a directed edge from $(x,y)$ to $(x+1,y)$, with non-negative integer value $d_{x,y}$, for all $1\le x < n, 1\le y \le n$, and there also exists a directed edge from $(x,y)$ to $(x,y+1)$, with non-negative integer value $r_{x,y}$, for all $1\le x \le n, 1\le y < n$.

Initially, you are at $(1,1)$, with an empty set $S$. You need to walk along the edges and eventually reach $(n,n)$. Whenever you pass an edge, its value will be inserted into $S$. Please maximize the MEX$^{\text{∗}}$ of $S$ when you reach $(n,n)$.

$^{\text{∗}}$The MEX (minimum excluded) of an array is the smallest non- negative integer that does not belong to the array. For instance:

  * The MEX of $[2,2,1]$ is $0$, because $0$ does not belong to the array.    * The MEX of $[3,1,0,1]$ is $2$, because $0$ and $1$ belong to the array, but $2$ does not.    * The MEX of $[0,3,1,2]$ is $4$, because $0, 1, 2$, and $3$ belong to the array, but $4$ does not.

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

The first line of each test case contains a single integer $n$ ($2\le n\le20$) — the number of rows and columns.

Each of the next $n-1$ lines contains $n$ integers separated by single spaces — the matrix $d$ ($0\le d_{x,y}\le 2n-2$).

Each of the next $n$ lines contains $n-1$ integers separated by single spaces — the matrix $r$ ($0\le r_{x,y}\le 2n-2$).

It is guaranteed that the sum of all $n^3$ does not exceed $8000$.

Output Format:
For each test case, print a single integer — the maximum MEX of $S$ when you reach $(n,n)$.

Tags:
['bitmasks', 'brute force', 'hashing', 'meet-in-the-middle']

Time Limit: 7000 ms
Memory Limit: 1024 MB
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind.

Your predecessors had taken their final bow; it's your turn to take the stage.

Sorting through some notes that were left behind, you found a curious statement named Problem 101:

  * Given a positive integer sequence $a_1,a_2,\ldots,a_n$, you can operate on it any number of times. In an operation, you choose three consecutive elements $a_i,a_{i+1},a_{i+2}$, and merge them into one element $\max(a_i+1,a_{i+1},a_{i+2}+1)$. Please calculate the maximum number of operations you can do without creating an element greater than $m$. 

After some thought, you decided to propose the following problem, named Counting 101:

  * Given $n$ and $m$. For each $k=0,1,\ldots,\left\lfloor\frac{n-1}{2}\right\rfloor$, please find the number of integer sequences $a_1,a_2,\ldots,a_n$ with elements in $[1, m]$, such that when used as input for Problem 101, the answer is $k$. As the answer can be very large, please print it modulo $10^9+7$.

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

The only line of each test case contains two integers $n$, $m$ ($1\le n\le 130$, $1\le m\le 30$).

Output Format:
For each test case, output $\left\lfloor\frac{n+1}{2}\right\rfloor$ numbers. The $i$-th number is the number of valid sequences such that when used as input for Problem 101, the answer is $i-1$, modulo $10^9+7$.

Tags:
['greedy']

Time Limit: 10100 ms
Memory Limit: 1010 MB
Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that:

  * $k \ge 2$.    * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$.    * For all $1 \le i < j \le k$, the first character of $t_i$ isn't equal to the last character of $t_j$. 

Turtle is given a string $s$ consisting of lowercase Latin letters. Please tell him whether the string $s$ is a good string!

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

The first line of each test case contains a single integer $n$ ($2 \le n \le 100$) — the length of the string.

The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters.

Output Format:
For each test case, output "YES" if the string $s$ is a good string, and "NO" 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:
['greedy', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).

The game goes as follows:

  * Let the current length of the sequence be $m$. If $m = 1$, the game ends.    * If the game does not end and it's Turtle's turn, then Turtle must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\max(a_i, a_{i + 1})$, and remove $a_{i + 1}$.    * If the game does not end and it's Piggy's turn, then Piggy must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\min(a_i, a_{i + 1})$, and remove $a_{i + 1}$. 

Turtle wants to maximize the value of $a_1$ in the end, while Piggy wants to minimize the value of $a_1$ in the end. Find the value of $a_1$ in the end if both players play optimally.

You can refer to notes for further clarification.

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 10^5$) — the length of the sequence.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) — the elements of the sequence $a$.

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 value of $a_1$ in the end if both players play optimally.

Tags:
['games', 'greedy', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB