inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{∗}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, there are no operators between the booleans.

Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of $n-1$ turns since there are $n$ booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally.

To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false:

  * If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation.    * Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation. 

For example, the expression true or false and false is evaluated as true or (false and false) $=$ true or false $=$ true. It can be shown that the result of any compound statement is unique.

$^{\text{∗}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$

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

The first line of each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the length of the string.

The second line contains a binary string of length $n$, consisting of characters $\texttt{0}$ and $\texttt{1}$ — the list of boolean values.

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

Output Format:
For each testcase, output "YES" (without quotes) if Alice wins, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

Tags:
['brute force', 'games', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB
QED is given a permutation$^{\text{∗}}$ $p$ of length $n$. He also has a string $s$ of length $n$ containing only characters $\texttt{L}$ and $\texttt{R}$. QED only likes permutations that are sorted in non-decreasing order. To sort $p$, he can select any of the following operations and perform them any number of times:

  * Choose an index $i$ such that $s_i = \texttt{L}$. Then, swap $p_i$ and $p_{i-1}$. It is guaranteed that $s_1 \neq \texttt{L}$.    * Choose an index $i$ such that $s_i = \texttt{R}$. Then, swap $p_i$ and $p_{i+1}$. It is guaranteed that $s_n \neq \texttt{R}$. 

He is also given $q$ queries. In each query, he selects an index $i$ and changes $s_i$ from $\texttt{L}$ to $\texttt{R}$ (or from $\texttt{R}$ to $\texttt{L}$). Note that the changes are persistent.

After each query, he asks you if it is possible to sort $p$ in non- decreasing order by performing the aforementioned operations any number of times. Note that before answering each query, the permutation $p$ is reset to its original form.

$^{\text{∗}}$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:
The first line contains $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $q$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq q \leq 2 \cdot 10^5$) – the length of the permutation and the number of queries.

The following line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$, $p$ is a permutation).

The following line contains $n$ characters $s_1s_2 \ldots s_n$. It is guaranteed that $s_i$ is either $\texttt{L}$ or $\texttt{R}$, $s_1 = \texttt{R}$, and $s_n = \texttt{L}$.

The following $q$ lines contain an integer $i$ ($2 \leq i \leq n-1$), denoting that $s_i$ is changed from $\texttt{L}$ to $\texttt{R}$ (or from $\texttt{R}$ to $\texttt{L}$).

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

Output Format:
For each query, output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

Tags:
['data structures', 'implementation', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Suppose we partition the elements of an array $b$ into any number $k$ of non-empty multisets $S_1, S_2, \ldots, S_k$, where $k$ is an arbitrary positive integer. Define the score of $b$ as the maximum value of $\operatorname{MEX}(S_1)$$^{\text{∗}}$$ + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}(S_k)$ over all possible partitions of $b$ for any integer $k$.

Envy is given an array $a$ of size $n$. Since he knows that calculating the score of $a$ is too easy for you, he instead asks you to calculate the sum of scores of all $2^n - 1$ non-empty subsequences of $a$.$^{\text{†}}$ Since this answer may be large, please output it modulo $998\,244\,353$.

$^{\text{∗}}$$\operatorname{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ that does not occur in the collection $c$. For example, $\operatorname{MEX}([0,1,2,2]) = 3$ and $\operatorname{MEX}([1,2,2]) = 0$

$^{\text{†}}$A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deleting several (possibly, zero or all) elements.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < n$) — the elements of the array $a$.

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 the answer, modulo $998\,244\,353$.

Tags:
['combinatorics', 'data structures', 'dp', 'greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
Suppose you have an array $b$. Initially, you also have a set $S$ that contains all distinct elements of $b$. The array $b$ is called orangutan-approved if it can be emptied by repeatedly performing the following operation:

  * In one operation, select indices $l$ and $r$ ($1 \leq l \leq r \leq |b|$) such that $v = b_l = b_{l+1} = \ldots = b_r$ and $v$ is present in $S$. Remove $v$ from $S$, and simultaneously remove all $b_i$ such that $l \leq i \leq r$. Then, reindex the elements $b_{r+1}, b_{r+2}, \ldots$ as $b_l, b_{l+1}, \ldots$ accordingly. 

You are given an array $a$ of length $n$ and $q$ queries.

Each query consists of two indices $l$ and $r$ ($1 \le l \le r \le n$), and you need to determine whether or not the subarray $a_{l}, a_{l+1}, \ldots, a_r$ is orangutan-approved.

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

The first line of each test case contains integers $n$ and $q$ ($1 \leq n,q \leq 2 \cdot 10^5$) — the size of $a$ and the number of queries, respectively.

The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array $a$.

The following $q$ lines contain two integers $l$ and $r$ — the endpoints of the subarray for each query ($1 \leq l \leq r \leq n$).

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

Output Format:
For each query, output "YES" (without quotes) if the subarray from $l$ to $r$ is orangutan-approved, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

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

Time Limit: 3000 ms
Memory Limit: 512 MB
This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved.

Orangutans are powerful beings—so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!

There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.

Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.

Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.

Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) — the number of planets in the universe.

The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) — the initial interval of vulnerability of the $i$-th planet.

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

Output Format:
For each test case, output an integer — the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB
This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved.

Orangutans are powerful beings—so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!

There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.

Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.

Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.

Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^6$) — the number of planets in the universe.

The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) — the initial interval of vulnerability of the $i$-th planet.

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

Output Format:
For each test case, output an integer — the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.

Tags:
['combinatorics', 'math']

Time Limit: 4000 ms
Memory Limit: 512 MB
Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:

  * Each light is connected to exactly two switches.    * Each switch is connected to exactly one light. It's unknown which light each switch is connected to.    * When all switches are off, all lights are also off.    * If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle. 

Alice brings the circuit, which shows only the states of the $2n$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?

Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — 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 50$) — the number of lights in the circuit.

The second line of each test case contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($0 \le a_i \le 1$) — the states of the switches in the circuit. $a_i = 0$ means the $i$-th switch is off, and $a_i = 1$ means the $i$-th switch is on.

Output Format:
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.

Tags:
['greedy', 'implementation', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given an array $a = [1, 2, \ldots, n]$, where $n$ is odd, and an integer $k$.

Your task is to choose an odd positive integer $m$ and to split $a$ into $m$ subarrays$^{\dagger}$ $b_1, b_2, \ldots, b_m$ such that:

  * Each element of the array $a$ belongs to exactly one subarray.    * For all $1 \le i \le m$, $|b_i|$ is odd, i.e., the length of each subarray is odd.    * $\operatorname{median}([\operatorname{median}(b_1), \operatorname{median}(b_2), \ldots, \operatorname{median}(b_m)]) = k$, i.e., the median$^{\ddagger}$ of the array of medians of all subarrays must equal $k$. $\operatorname{median}(c)$ denotes the median of the array $c$. 

$^{\dagger}$A subarray of the array $a$ of length $n$ is the array $[a_l, a_{l + 1}, \ldots, a_r]$ for some integers $1 \le l \le r \le n$.

$^{\ddagger}$A median of the array of odd length is the middle element after the array is sorted in non-decreasing order. For example: $\operatorname{median}([1,2,5,4,3]) = 3$, $\operatorname{median}([3,2,1]) = 2$, $\operatorname{median}([2,1,2,1,2,2,2]) = 2$.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 5000$) — 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 k \le n < 2 \cdot 10^5$, $n$ is odd) — the length of array $a$ and the desired median of the array of medians of all subarrays.

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:

  * If there is no suitable partition, output $-1$ in a single line.    * Otherwise, in the first line, output an odd integer $m$ ($1 \le m \le n$), and in the second line, output $m$ distinct integers $p_1, p_2 , p_3 , \ldots, p_m$ ($1 = p_1 < p_2 < p_3 < \ldots < p_m \le n$) — denoting the left borders of each subarray. 

In detail, for a valid answer $[p_1, p_2, \ldots, p_m]$:

  * $b_1 = \left[ a_{p_1}, a_{p_1 + 1}, \ldots, a_{p_2 - 1} \right]$    * $b_2 = \left[ a_{p_2}, a_{p_2 + 1}, \ldots, a_{p_3 - 1} \right]$    * $\ldots$    * $b_m = \left[ a_{p_m}, a_{p_m + 1}, \ldots, a_n \right]$. 

If there are multiple solutions, you can output any of them.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$.

You can perform the following operation any number (possibly $0$) of times:

  * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$. 

Find the minimum number of operations required to make the array $a$ satisfy the condition:

  * For every pairwise distinct triplet of indices $(x, y, z)$ ($1 \le x, y, z \le n$, $x \ne y$, $y \ne z$, $x \ne z$), there exists a non-degenerate triangle with side lengths $a_x$, $a_y$ and $a_z$, i.e. $a_x + a_y > a_z$, $a_y + a_z > a_x$ and $a_z + a_x > a_y$.

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$ ($3 \le n \le 2 \cdot 10^5$) — the number of elements in 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 $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the minimum number of operations required.

Tags:
['binary search', 'math', 'sortings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is an interactive problem.

Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.

The monster's nerve system can be structured as a tree$^{\dagger}$ of $n$ nodes (really, everything should stop resembling trees all the time$\ldots$), numbered from $0$ to $n-1$, with node $0$ as the root.

Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values $p_1, p_2, \ldots, p_{n-1}$ of the tree, where $p_i$ ($0 \le p_i < i$) is the direct parent node of node $i$ ($1 \le i \le n - 1$).

She doesn't know exactly how the nodes are placed, but she knows a few convenient facts:

  * If we remove root node $0$ and all adjacent edges, this tree will turn into a forest consisting of only paths$^{\ddagger}$. Each node that was initially adjacent to the node $0$ will be the end of some path.    * The nodes are indexed in a way that if $1 \le x \le y \le n - 1$, then $p_x \le p_y$.    * Node $1$ has exactly two adjacent nodes (including the node $0$). 

![](CDN_BASE_URL/9e3efde5fa1c3b92802e71707f427f91)| ![](CDN_BASE_URL/ba6c28030157828dcd682786de7f0527)| ![](CDN_BASE_URL/bdeedf002bf19f7cf8b52abe474df823)   ---|---|---   The tree in this picture does not satisfy the condition, because if we remove node $0$, then node $2$ (which was initially adjacent to the node $0$) will not be the end of the path $4-2-5$.| The tree in this picture does not satisfy the condition, because $p_3 \le p_4$ must hold.| The tree in this picture does not satisfy the condition, because node $1$ has only one adjacent node.      Gretel can make queries to the containment cell:

  * "? a b" ($1 \le a, b < n$, $a \ne b$) — the cell will check if the simple path between nodes $a$ and $b$ contains the node $0$. 

However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most $2n - 6$ times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand?

$^{\dagger}$A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them.

$^{\ddagger}$A path is a tree whose vertices can be listed in the order $v_1, v_2, \ldots, v_k$ such that the edges are $(v_i, v_{i+1})$ ($1 \le i < k$).

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

The first line of each test case contains a single integer $n$ ($4 \le n \le 10^4$) — the number of nodes in Genokraken's nerve system.

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

Output Format:


Tags:
['constructive algorithms', 'data structures', 'graphs', 'implementation', 'interactive', 'trees', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a cyclic array $a$ with $n$ elements, where $n$ is odd. In each operation, you can do the following:

  * Choose an index $1 \le i \le n$ and increase $a_{i - 1}$ by $1$, $a_i$ by $2$, and $a_{i + 1}$ by $1$. The element before the first element is the last element because this is a cyclic array. 

A cyclic array is called balanced if all its elements are equal to each other.

Find any sequence of operations to make this cyclic array balanced or determine that it is impossible. Please note that you do not have to minimize the number of operations.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 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 \le n < 2 \cdot 10^5$, $n$ is odd) — 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 $2 \cdot 10^5$.

Output Format:
For each test case:

  * If it is impossible to make the cyclic array balanced, output $-1$.    * Otherwise, output $n$ integers $v_1, v_2, \ldots, v_n$ ($0 \leq v_i \leq 10^{18}$) — where $v_i$ denotes the number of operations applied to index $i$. It can be proved that if any solution exists, then there exists a solution under the given constraints. If there are several solutions under the given constraints, output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Having the magical beanstalk, Jack has been gathering a lot of peanuts lately. Eventually, he has obtained $n$ pockets of peanuts, conveniently numbered $1$ to $n$ from left to right. The $i$-th pocket has $a_i$ peanuts.

Jack and his childhood friend Alice decide to play a game around the peanuts. First, Alice divides the pockets into some boxes; each box will have a non- zero number of consecutive pockets, and each pocket will, obviously, belong to exactly one box. At the same time, Alice does not change the order of the boxes, that is, the boxes are numbered in ascending order of the indices of the pockets in them.

After that, Alice and Jack will take turns alternately, with Alice going first.

At each turn, the current player will remove a positive number of peanuts from exactly one pocket which belongs to the leftmost non-empty box (i.e., the leftmost box containing at least one non-empty pocket). In other words, if we number the boxes from left to right, then each player can only pick peanuts from the pocket in the $j$-th box ($j \ge 2$) only if the $(j - 1)$-th box has no peanuts left. The player who cannot make a valid move loses.

Alice is sure she will win since she has the advantage of dividing the pockets into boxes herself. Thus, she wanted to know how many ways there are for her to divide the peanuts into boxes at the start of the game so that she will win, assuming both players play optimally. Can you help her with the calculation?

As the result can be very large, output it modulo $998\,244\,353$.

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 \leq n \leq 10^6$) — the number of pockets.

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 number of peanuts in each pocket.

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

Output Format:
For each test case, output a single integer — the number of ways for Alice to divide the pockets into boxes at the start of the game to guarantee her win, assuming both players play optimally, modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'games', 'math']

Time Limit: 4000 ms
Memory Limit: 256 MB