text
stringlengths
0
801
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$.
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.
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$.
In the first testcase, there are seven non-empty subsets of planets we must consider:
* For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interv
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$.
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$.
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
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
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there.
Kosuke is very afraid of water, so Sakurako needs to help him:
* With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one.
More formally, she can choose a submatrix with the upper left corner located at $(i, j)$ and the lower right corner at $(p, q)$, such that $p-i=q-j$. She can then add one to each element at the intersection of the $(i + k)$-th row and the $(j + k)$-th column, for all $k$ such that $0 \le k \le p-i$.
Determine the minimum number of times Sakurako must use her magic so that there are no lakes.
The first line contains a single integer $t$ ($1 \le t \le 200$) — the number of test cases.
Each test case is described as follows:
* The first line of each test case consists of a single number $n$ ($1 \le n \le 500$). * Each of the following $n$ lines consists of $n$ integers separated by spaces, which correspond to the heights of the mountains in the valley $a$ ($-10^5 \le a_{i,j} \le 10^5$).
It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.
Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of students.
The disturbance of the line is defined as the number of neighbouring people with the same topic of interest. In other words, disturbance is the number of indices $j$ ($1 \le j < n$) such that $a_j = a_{j + 1}$.
In order to do this, you can choose index $i$ ($1\le i\le n$) and swap students at positions $i$ and $n-i+1$. You can perform any number of swaps.
Your task is to determine the minimal amount of disturbance that you can achieve by doing the operation described above any number of times.
The first line contains one integer $t$ ($1\le t\le 10^4$) — the number of test cases.
Each test case is described by two lines.
* The first line contains one integer $n$ ($2 \le n \le 10^5$) — the length of the line of students. * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) — the topics of interest of students in line.
It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output the minimal possible disturbance of the line that you can achieve.