problem_id
stringlengths 5
6
| problem_title
stringlengths 6
50
| difficulty
stringclasses 3
values | problem_statement
stringlengths 719
6.37k
| platform
stringclasses 1
value | link
stringlengths 48
49
|
---|---|---|---|---|---|
2035H | H. Peak Productivity Forces | hard | I'm peakly productive and this is deep.
You are given two permutations$^{\text{∗}}$ $a$ and $b$, both of length $n$.
You can perform the following three-step operation on permutation $a$:
1. Choose an index $i$ ($1 \le i \le n$).
2. Cyclic shift $a_1, a_2, \ldots, a_{i-1}$ by $1$ to the right. If you had chosen $i = 1$, then this range doesn't exist, and you cyclic shift nothing.
3. Cyclic shift $a_{i + 1}, a_{i + 2}, \ldots, a_n$ by $1$ to the right. If you had chosen $i = n$, then this range doesn't exist, and you cyclic shift nothing.
After the operation, $a_1,a_2,\ldots, a_{i-2},a_{i-1},a_i,a_{i + 1}, a_{i + 2},\ldots,a_{n-1}, a_n$ is transformed into $a_{i-1},a_1,\ldots,a_{i-3},a_{i-2},a_i,a_n, a_{i + 1},\ldots,a_{n-2}, a_{n-1}$.
Here are some examples of operations done on the identity permutation $[1,2,3,4,5,6,7]$ of length $7$:
- If we choose $i = 3$, it will become $[2, 1, 3, 7, 4, 5, 6]$.
- If we choose $i = 1$, it will become $[1, 7, 2, 3, 4, 5, 6]$.
- If we choose $i = 7$, it will become $[6, 1, 2, 3, 4, 5, 7]$.
Notably, position $i$ is not shifted.
Find a construction using at most $2n$ operations to make $a$ equal to $b$ or print $-1$ if it is impossible. The number of operations does not need to be minimized. It can be shown that if it is possible to make $a$ equal to $b$, it is possible to do this within $2n$ operations.
$^{\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
The first line contains a single integer $t$ ($1 \le t \le 5 \cdot 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the lengths of permutations $a$ and $b$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the values of permutation $a$.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$) — the values of permutation $b$.
It is guaranteed that the sum of $n$ over all test cases will not exceed $5 \cdot 10^5$.
### Output
For each test case:
If there is a sequence of operations to transform $a$ into $b$, output a single integer $q$ ($0\le q\le 2n$) — the number of operations in the first line and $q$ integers with the $i$-th number representing the index of the $i$-th operation in the second line.
If there is no sequence of operations, output $-1$ in the only line.
### Example
#### Input #1
```
4
1
1
1
2
1 2
2 1
3
2 1 3
3 2 1
8
7 8 3 5 4 6 1 2
2 1 6 4 5 3 8 7
```
#### Output #1
```
0
-1
2
1 3
7
3 4 5 1 2 1 1
```
### Note
In the first case, you can do no operation since $a=b$.
In the second case, it can be proved $a$ can not be transformed into $b$.
In the third case, $a$ is transformed into $[2,3,1]$ after the first operation and into $b$ after the second operation. | codeforces | https://codeforces.com/problemset/problem/2035/H |
2035G2 | G2. Go Learn! (Hard Version) | hard | The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3\cdot 10^5$ and the sum of $n$ does not exceed $10^6$. You can only make hacks if both versions are solved.
Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.
Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.
```
`let l = 1<br/>let h = n<br/><br/>while l < h:<br/> let m = floor((l + h) / 2)<br/><br/> if a[m] < k:<br/> l = m + 1<br/> else:<br/> h = m<br/><br/>return l<br/>````
Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.
Test $i$ is correct if the following hold:
1. The $x_i$-th element in the array is $k_i$.
2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$.
It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.
In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq m \leq n \leq 3 \cdot 10^5$) denoting the number of the array and the number of tests.
The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.
It is guaranteed the sum of $n$ across all test cases does not exceed $10^6$.
### Output
For each test case, output two integers, $r$ — the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.
### Examples
#### Input #1
```
2
5 4
1 1
2 2
4 3
5 4
5 4
5 4
2 5
1 2
3 3
```
#### Output #1
```
0 1
1 3
```
#### Input #2
```
3
6 6
1 3
2 5
3 1
4 2
5 4
6 6
30 8
19 22
6 12
12 1
28 27
3 4
14 25
29 14
11 15
300000 1
5 10
```
#### Output #2
```
3 78
3 839271911
0 702730519
```
### Note
Consider the first example.
In the first test case, the array $[1,2,2,3,4]$ satisfies all $m$ tests, so the minimum number of tests Bessie has to remove is $0$. Note that this is also the only array that satisfies all $m$ tests.
In the second test case, the minimum number of tests Bessie has to remove is $1$. The only test Bessie can remove is $(2,5)$. If Bessie removes test $(2,5)$, then the arrays satisfying the remaining $m-1$ tests are $[2,2,3,1,4]$, $[2,2,3,2,4]$, $[2,2,3,3,4]$. | codeforces | https://codeforces.com/problemset/problem/2035/G2 |
2035G1 | G1. Go Learn! (Easy Version) | hard | The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved.
Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.
Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.
```
`let l = 1<br/>let h = n<br/><br/>while l < h:<br/> let m = floor((l + h) / 2)<br/><br/> if a[m] < k:<br/> l = m + 1<br/> else:<br/> h = m<br/><br/>return l<br/>````
Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.
Test $i$ is correct if the following hold:
1. The $x_i$-th element in the array is $k_i$.
2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$.
It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.
In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests.
The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.
It is guaranteed the sum of $n$ across all test cases does not exceed $10^4$.
### Output
For each test case, output two integers, $r$ — the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.
### Examples
#### Input #1
```
2
5 4
1 1
2 2
4 3
5 4
5 4
5 4
2 5
1 2
3 3
```
#### Output #1
```
0 1
1 3
```
#### Input #2
```
2
6 6
1 3
2 5
3 1
4 2
5 4
6 6
30 8
19 22
6 12
12 1
28 27
3 4
14 25
29 14
11 15
```
#### Output #2
```
3 78
3 839271911
```
### Note
Consider the first example.
In the first test case, the array $[1,2,2,3,4]$ satisfies all $m$ tests, so the minimum number of tests Bessie has to remove is $0$. Note that this is also the only array that satisfies all $m$ tests.
In the second test case, the minimum number of tests Bessie has to remove is $1$. The only test Bessie can remove is $(2,5)$. If Bessie removes test $(2,5)$, then the arrays satisfying the remaining $m-1$ tests are $[2,2,3,1,4]$, $[2,2,3,2,4]$, $[2,2,3,3,4]$. | codeforces | https://codeforces.com/problemset/problem/2035/G1 |
2027E2 | E2. Bit Game (Hard Version) | hard | This is the hard version of this problem. The only difference is that you need to output the number of choices of games where Bob wins in this version, where the number of stones in each pile are not fixed. You must solve both versions to be able to hack.
Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:
- $1 \le d \le a_i$, and
- $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The player who cannot make a move loses, and Alice goes first.
You're given the $a_i$ values of each pile, but the number of stones in the $i$-th pile has not been determined yet. For the $i$-th pile, $x_i$ can be any integer between $1$ and $b_i$, inclusive. That is, you can choose an array $x_1, x_2, \ldots, x_n$ such that the condition $1 \le x_i \le b_i$ is satisfied for all piles.
Your task is to count the number of games where Bob wins if both players play optimally. Two games are considered different if the number of stones in any pile is different, i.e., the arrays of $x$ differ in at least one position.
Since the answer can be very large, please output the result modulo $10^9 + 7$.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains $n$ ($1 \le n \le 10^4$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i < 2^{30}$).
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i < 2^{30}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
### Output
Output a single integer, the number of games where Bob wins, modulo $10^9 + 7$.
### Example
#### Input #1
```
7
3
1 2 3
3 2 2
1
13
45
5
5 4 7 8 6
4 4 5 5 5
4
6 4 8 8
12 13 14 12
3
92856133 46637598 12345678
29384774 73775896 87654321
2
65 12
110 31
4
677810235 275091182 428565855 720629731
74522416 889934149 3394714 230851724
```
#### Output #1
```
4
4
0
6552
722019507
541
665443265
```
### Note
In the first test case, no matter which values of $x_2$ and $x_3$ we choose, the second and third piles will always be chosen exactly once before no more stones can be taken from them. If $x_1 = 2$, then no stones can be taken from it, so Bob will make the last move. If $x_1 = 1$ or $x_1 = 3$, then exactly one move can be made on that pile, so Alice will make the last move. So Bob wins when $x = [2, 1, 1]$ or $x = [2, 1, 2]$ or $x = [2, 2, 1]$ or $x = [2, 2, 2]$.
In the second test case, Bob wins when $x_1 = 14$ or $x_1 = 30$ by removing $14 - k$ stones, where $k$ is the number of stones Alice removes on her turn. Bob also wins when $x_1 = 16$ or $x_1 = 32$ since Alice does not have any moves to begin with. | codeforces | https://codeforces.com/problemset/problem/2027/E2 |
2023E | E. Tree of Life | hard | 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
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
For each test case, output a single integer — the minimum number of paths that the sages need to select to prevent a catastrophe.
### Example
#### Input #1
```
5
4
1 2
2 3
3 4
2
1 2
4
1 2
1 3
1 4
8
3 7
2 4
1 2
2 5
3 6
1 3
3 8
6
2 3
1 2
3 6
1 5
1 4
```
#### Output #1
```
1
0
3
7
3
```
### Note
In the first test case, there are two pairs of channels emanating from a single node: $(1, 2)$ and $(2, 3)$, $(2, 3)$ and $(3, 4)$. It is sufficient to perform the ritual along the path $1-2-3-4$. Thus, the answer is $1$.
In the second test case, there are no pairs of channels emanating from a single node, so the answer is $0$.
In the third test case, rituals can be performed along the paths $2-1-3$, $2-1-4$, and $3-1-4$. | codeforces | https://codeforces.com/problemset/problem/2023/E |
2023F | F. Hills and Pits | hard | 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
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
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.
### Example
#### Input #1
```
5
1 1
-179
1 1
5 3
-2 2 -1 3 -1
2 4
1 5
1 3
7 1
1 1 1 -4 1 1 1
1 7
7 2
2 -2 2 -2 1 2 -1
1 7
2 7
4 4
1000000000 1000000000 999999999 -1000000000
2 4
3 4
2 3
1 3
```
#### Output #1
```
-1
2
5
-1
8
6
6
2
-1
1
2
```
### Note
In the first test case, $179$ units of sand need to be added to the only section. However, there is nowhere to take it from, so this is impossible.
In the second test case:
- In the first query, the dump truck can start its journey at the second section. It can take $2$ units of sand, after which the height in the second section will become $0$. Then the dump truck can move to the third section. It can pour $1$ unit of sand there, after which the height in the third section will become $0$. Then the dump truck can move to the fourth section. There it can take $3$ units of sand, after which the height in the fourth section will become $0$. In total, the dump truck will spend $2$ minutes on movements.
- In the second query, the dump truck can start its journey at the fourth section. It can take $3$ units of sand, after which the height in the fourth section will become $0$. Then the dump truck can move to the fifth section. It can pour $1$ unit of sand there, after which the height in the fifth section will become $0$. Then the dump truck can move back to the fourth section and then to the third. It can pour $1$ unit of sand there, after which the height in the third section will become $0$. Then the dump truck can move to the second section. It can take $2$ units of sand. Then it can move to the first section. It can pour $2$ units of sand there, after which the height in the first section will become $0$. In total, the dump truck will spend $5$ minutes on movements.
- In the third query, the dump truck will not be able to make the height in each section equal to $0$. | codeforces | https://codeforces.com/problemset/problem/2023/F |
2030G2 | G2. The Destruction of the Universe (Hard Version) | hard | 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
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
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$.
### Example
#### Input #1
```
3
3
1 1
2 3
3 3
4
1 4
2 3
2 4
1 1
5
1 2
2 3
3 4
4 5
1 5
```
#### Output #1
```
5
6
24
```
### Note
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' interval of vulnerability.
- For the subset $\{[1,1], [2,3]\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval.
- For the subset $\{[1,1], [3,3]\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval.
- For the subset $\{[1,1], [2,3], [3,3]\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$. | codeforces | https://codeforces.com/problemset/problem/2030/G2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.