inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
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     let h = n          while l < h:       let m = floor((l + h) / 2)            if a[m] < k:         l = m + 1       else:         h = m          return l     

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

Tags:
['divide and conquer', 'dp']

Time Limit: 5000 ms
Memory Limit: 256 MB
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 Format:
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 Format:
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.

Tags:
['constructive algorithms']

Time Limit: 3000 ms
Memory Limit: 256 MB
Boris Notkin composes melodies. He represents them as a sequence of notes, where each note is encoded as an integer from $0$ to $127$ inclusive. The interval between two notes $a$ and $b$ is equal to $|a - b|$ semitones.

Boris considers a melody perfect if the interval between each two adjacent notes is either $5$ semitones or $7$ semitones.

After composing his latest melodies, he enthusiastically shows you his collection of works. Help Boris Notkin understand whether his melodies are perfect.

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

Each melody is described by two lines.

The first line contains an integer $n$ ($2 \leq n \leq 50$) — the number of notes in the melody.

The second line contains $n$ integers $a_{1}, a_{2}, \dots, a_{n}$ ($0 \leq a_{i} \leq 127$) — the notes of the melody.

Output Format:
For each melody, output "YES", if it is perfect; otherwise, output "NO".

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:
['implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB
Arseniy came up with another business plan — to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$.

You can place any number of bottles on each shelf, but all bottles on the same shelf must be of the same brand.

Arseniy knows that all the bottles he puts on the shelves of the machine will be sold. Therefore, he asked you to calculate the maximum amount he can earn.

Input Format:
The first line contains one 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 $k$ ($1 \le n, k \le 2 \cdot 10^5$), where $n$ is the number of shelves in the machine, and $k$ is the number of bottles available to Arseniy.

The next $k$ lines contain two integers $b_i$ and $c_i$ ($1 \le b_i \le k, 1 \le c_i \le 1000$) — the brand and cost of the $i$-th bottle.

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

Output Format:
For each test case, output one integer — the maximum amount that Arseniy can earn.

Tags:
['greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones.

Now she wants to make it even more beautiful by performing $q$ operations on it.

Each operation is described by two integers $i$ ($1 \le i \le |s|$) and $v$ ($v \in \\{0, 1\\}$) and means that the $i$-th character of the string is assigned the value $v$ (that is, the assignment $s_i = v$ is performed).

But Anya loves the number $1100$, so after each query, she asks you to tell her whether the substring "1100" is present in her string (i.e. there exist such $1 \le i \le |s| - 3$ that $s_{i}s_{i + 1}s_{i + 2}s_{i + 3} = \texttt{1100}$).

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

The first line of the test case contains the string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$), consisting only of the characters "0" and "1". Here $|s|$ denotes the length of the string $s$.

The next line contains an integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) — the number of queries.

The following $q$ lines contain two integers $i$ ($1 \leq i \leq |s|$) and $v$ ($v \in \\{0, 1\\}$), describing the query.

It is guaranteed that the sum of $|s|$ across all test cases does not exceed $2 \cdot 10^5$. It is also guaranteed that the sum of $q$ across all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO".

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:
['brute force', 'implementation']

Time Limit: 3000 ms
Memory Limit: 256 MB
One morning, Polycarp woke up and realized that $1543$ is the most favorite number in his life.

The first thing that Polycarp saw that day as soon as he opened his eyes was a large wall carpet of size $n$ by $m$ cells; $n$ and $m$ are even integers. Each cell contains one of the digits from $0$ to $9$.

Polycarp became curious about how many times the number $1543$ would appear in all layers$^{\text{∗}}$ of the carpet when traversed clockwise.

$^{\text{∗}}$The first layer of a carpet of size $n \times m$ is defined as a closed strip of length $2 \cdot (n+m-2)$ and thickness of $1$ element, surrounding its outer part. Each subsequent layer is defined as the first layer of the carpet obtained by removing all previous layers from the original carpet.

Input Format:
The first line of the input contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The following lines describe the test cases.

The first line of each test case contains a pair of numbers $n$ and $m$ ($2 \leq n, m \leq 10^3$, $n, m$ — even integers).

This is followed by $n$ lines of length $m$, consisting of digits from $0$ to $9$ — the description of the carpet.

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

Output Format:
For each test case, output a single number — the total number of times $1543$ appears in all layers of the carpet in the order of traversal clockwise.

Tags:
['brute force', 'implementation', 'matrices']

Time Limit: 2000 ms
Memory Limit: 256 MB
A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy — that's what sages do.

There are $n$ countries, each with exactly $k$ regions. For the $j$-th region of the $i$-th country, they calculated the value $a_{i,j}$, which reflects the amount of water in it.

The sages intend to create channels between the $j$-th region of the $i$-th country and the $j$-th region of the $(i + 1)$-th country for all $1 \leq i \leq (n - 1)$ and for all $1 \leq j \leq k$.

Since all $n$ countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the $j$-th region of the $i$-th country will be $b_{i,j} = a_{1,j} | a_{2,j} | ... | a_{i,j}$, where $|$ denotes the [bitwise "OR"](http://tiny.cc/bitwise_or) operation.

After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you $q$ queries for consideration.

Each query will contain $m$ requirements.

Each requirement contains three parameters: the region number $r$, the sign $o$ (either "$<$" or "$>$"), and the value $c$. If $o$ = "$<$", then in the $r$-th region of the country you choose, the new value must be strictly less than the limit $c$, and if $o$ = "$>$", it must be strictly greater.

In other words, the chosen country $i$ must satisfy all $m$ requirements. If in the current requirement $o$ = "$<$", then it must hold that $b_{i,r} < c$, and if $o$ = "$>$", then $b_{i,r} > c$.

In response to each query, you should output a single integer — the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output $-1$.

Input Format:
The first line contains three integers $n$, $k$, and $q$ ($1 \leq n, k, q \leq 10^5$) — the number of countries, regions, and queries, respectively.

Next, there are $n$ lines, where the $i$-th line contains $k$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,k}$ ($1 \leq a_{i,j} \leq 10^9$), where $a_{i,j}$ is the value of the $j$-th region of the $i$-th country.

Then, $q$ queries are described.

The first line of each query contains a single integer $m$ ($1 \leq m \leq 10^5$) — the number of requirements.

Then follow $m$ lines, each containing an integer $r$, a character $o$, and an integer $c$ ($1 \leq r \leq k$, $0 \leq c \leq 2 \cdot 10^9$), where $r$ and $c$ are the region number and the value, and $o$ is either "$<$" or "$>$" — the sign.

It is guaranteed that $n \cdot k$ does not exceed $10^5$ and that the sum of $m$ across all queries also does not exceed $10^5$.

Output Format:
For each query, output a single integer on a new line — the smallest number of the suitable country, or $-1$ if no such country exists.

Tags:
['binary search', 'constructive algorithms', 'data structures', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB
Alice has been giving gifts to Bob for many years, and she knows that what he enjoys the most is performing [bitwise XOR](http://tiny.cc/xor_wiki_eng) of interesting integers. Bob considers a positive integer $x$ to be interesting if it satisfies $x \not\equiv k (\bmod 2^i)$. Therefore, this year for his birthday, she gifted him a super-powerful "XORificator 3000", the latest model.

Bob was very pleased with the gift, as it allowed him to instantly compute the XOR of all interesting integers in any range from $l$ to $r$, inclusive. After all, what else does a person need for happiness? Unfortunately, the device was so powerful that at one point it performed XOR with itself and disappeared. Bob was very upset, and to cheer him up, Alice asked you to write your version of the "XORificator".

Input Format:
The first line of input contains a single integer $t$ $(1 \leq t \leq 10^4)$ — the number of XOR queries on the segment. The following $t$ lines contain the queries, each consisting of the integers $l$, $r$, $i$, $k$ $(1 \leq l \leq r \leq 10^{18}$, $0 \leq i \leq 30$, $0 \leq k < 2^i)$.

Output Format:
For each query, output a single integer — the XOR of all integers $x$ in the range $[l, r]$ such that $x \not\equiv k \mod 2^i$.

Tags:
['bitmasks', 'dp', 'number theory', 'two pointers']

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

The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania — $n$ ($3 \leq n \leq 10^{18}$) types of books, numbered from $1$ to $n$. Each book's type number is indicated on its spine. Moreover, each type of book is stored in the library in exactly two copies! And you have been appointed as the librarian.

One night, you wake up to a strange noise and see a creature leaving the building through a window. Three thick tomes of different colors were sticking out of the mysterious thief's backpack. Before you start searching for them, you decide to compute the numbers $a$, $b$, and $c$ written on the spines of these books. All three numbers are distinct.

So, you have an unordered set of tomes, which includes one tome with each of the pairwise distinct numbers $a$, $b$, and $c$, and two tomes for all numbers from $1$ to $n$, except for $a$, $b$, and $c$. You want to find these values $a$, $b$, and $c$.

Since you are not working in a simple library, but in the Library of Magic, you can only use one spell in the form of a query to check the presence of books in their place:

  * "xor l r" — Bitwise XOR query with parameters $l$ and $r$. Let $k$ be the number of such tomes in the library whose numbers are greater than or equal to $l$ and less than or equal to $r$. You will receive the result of the computation $v_1 \oplus v_2 \oplus ... \oplus v_k$, where $v_1 ... v_k$ are the numbers on the spines of these tomes, and $\oplus$ denotes the operation of [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng). 

Since your magical abilities as a librarian are severely limited, you can make no more than $150$ queries.

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

The first line of each test case contains a single integer $n$ ($3 \leq n \leq 10^{18}$) — the number of types of tomes.

Output Format:


Tags:
['binary search', 'bitmasks', 'constructive algorithms', 'divide and conquer', 'flows', 'geometry', 'interactive', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB