You are given $$$n$$$ intervals $$$[l_i, r_i]$$$. Assign a charge of $$$+1$$$ or $$$-1$$$ to each interval such that for every integer $$$i$$$ the following holds:
The sum of charges of intervals containing $$$i$$$ is $$$+1$$$, $$$-1$$$ or $$$0$$$. If no intervals contain $$$i$$$, this sum of charges is considered to be $$$0$$$.
The interval $$$[l, r]$$$ is said to contain an integer $$$j$$$ if and only if $$$l \le j \le r$$$.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of each test case follows.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of intervals.
The next $$$n$$$ lines each contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l \le r \le 2 \cdot 10^5$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case, if there doesn't exist an assignment that satisfies the above constraints, print NO. Otherwise, print YES followed by $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$c_i \in \{-1, 1\}$$$) on a new line — the charges assigned to the intervals in the order that they appear in the input.
4410 278 246 1921 22112 1232 2611 1417 2928 2322 29
YES 1 -1 1 1 YES 1 YES 1 -1 -1 YES 1 -1
Alice has $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$. Bob wants to secretly modify these integers by picking a positive integer $$$x$$$ and replacing $$$a_i$$$ with $$$a_i | x$$$ for every valid $$$i$$$. Here, $$$|$$$ represents the bitwise OR operation.
Alice doesn't notice any change if the resulting sequence is a permutation of $$$a$$$. Find the number of choices of $$$x$$$ such that Bob's modification goes unnoticed by Alice.
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of integers that Alice has.
The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer on a new line, the answer to the problem.
2515 9 11 25 1343 5 9 17
3 1
Consider having two stacks of integers, one loaded with some distinct integers and the other empty. The following operation is performed until both stacks become empty:
For a sequence $$$S$$$ of distinct integers, define $$$f(S)$$$ as the number of operations performed if the first stack is loaded with $$$S$$$.
Given a permutation $$$p$$$ of length $$$n$$$, compute the sum of $$$f(S)$$$ over all non-empty subsequences $$$S$$$ of $$$p$$$.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$. It is guaranteed that $$$p$$$ is a permutation of $$$1$$$ to $$$n$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer on a new line, the sum of $$$f(S)$$$ over all non-empty subsequences $$$S$$$ of $$$p$$$ modulo $$$10^9+7$$$.
532 3 142 4 1 354 1 3 5 221 273 5 4 7 1 6 2
14 44 121 5 734
Given a permutation $$$p$$$ of length $$$n$$$, consider the undirected simple graph $$$G$$$ with $$$n$$$ vertices constructed as follows.
Given an integer $$$K$$$ and $$$n$$$ non-negative integers $$$b_1, b_2, \dots b_n$$$, compute the number of non-empty subsequences $$$s$$$ of $$$\{1, 2, \dots n\}$$$ such that
$$$$$$b_{s_1} \oplus b_{s_2} \oplus \dots b_{s_{|s|}} = K$$$$$$
and the set of nodes $$$s$$$ forms an independent set in $$$G$$$. Here, $$$\oplus$$$ represents the bitwise XOR operation.
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$K$$$ ($$$2 \le n \le 10^3, 0 \le K \lt 2^{10}$$$).
The second and third lines of each test case contain the permutation $$$p$$$ and the sequence $$$b$$$ respectively ($$$1 \le p_i \le n, 0 \le b_i \lt 2^{10}$$$). It is guaranteed that $$$p$$$ is a permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^3$$$.
For each test case, print a single integer on a new line, the answer to the problem modulo $$$10^9 + 7$$$.
25 15 2 3 1 40 1 1 1 16 04 3 1 6 2 52 1 2 0 2 1
5 5
In the first test case, the $$$5$$$ independent sets satisfying the conditions above are $$$\{1\}$$$, $$$\{2\}$$$, $$$\{3\}$$$, $$$\{4\}$$$ and $$$\{1, 5\}$$$.
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform the following two kinds of operations.
Find any sequence of at most $$$20 n$$$ operations such that all elements of $$$a$$$ become equal after performing them.
The first line of the input 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 contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of elements in the array.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the elements of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, on the first line print $$$k$$$ ($$$0 \le k \le 20 n$$$) — the number of operations to be performed, followed by $$$k$$$ lines each of which are one of the following two kinds.
Note that at all times, every integer in $$$a$$$ must satisfy $$$-10^9 \le a_i \le 10^9$$$.
If there are multiple possible sequences of operations, you may print any of them.
354 19 5 7 113296 168 48299 99
11 2 3 1 1 1 1 1 1 1 1 1 3 1 3 1 3 1 4 1 4 1 5 7 2 40 1 2 1 3 1 3 1 3 1 3 1 3 0
For string $$$s$$$ of lowercase Latin letters, we start with an empty string $$$t$$$ and perform the following operation for each character $$$c$$$ of $$$s$$$ in left-to-right order:
The string $$$s$$$ is considered good if the corresponding string $$$t$$$ is empty after the above sequence of operations. Given a string $$$s$$$ count the number of good substrings of $$$s$$$.
The first line of the input contains $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
Each test case consists of a single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^6$$$).
It is guaranteed that the sum of $$$|s|$$$ over all test cases doesn't exceed $$$2 \cdot 10^6$$$.
For each test case, print a single integer — the number of good substrings of $$$s$$$.
5abbcbabbaappqpqqpqpzzzhelloosijekrreeerer
8 4 2 3 4
Given a string $$$s$$$ of lower case Latin letters and an integer $$$k$$$ ($$$1 \le k \le |s|$$$), construct a string $$$t$$$, also of lowercase Latin letters and of the same length as $$$s$$$, such that $$$\mathrm{LCS}(s, t) = k$$$.
Note: $$$\mathrm{LCS}(s, t)$$$ stands for the length of the longest common subsequence of strings $$$s$$$ and $$$t$$$.
The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^3)$$$ — the number of test cases.
The first line of each test case contains two integers $$$|s|$$$ and $$$k$$$ ($$$1 \le k \le |s| \le 5000$$$).
The second line of each test case contains the string $$$s$$$.
It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$5000$$$.
For each test case, if there exists any string $$$t$$$ satisfying the above constraints, print it. Otherwise print $$$-1$$$.
25 3apple5 1juice
pleaa aaase
You are given a string $$$s$$$ of lowercase Latin letters. You need to rearrange the characters of $$$s$$$ to form a new string $$$t$$$ such that the following value is maximized:
$$$$$$\min(\mathrm{LCS}(s, t), \mathrm{LCS}(s, \overline{t})).$$$$$$
Note:
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases.
Each test case consists of a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^3$$$) — the length of the string $$$s$$$ followed by $$$s$$$ on a new line.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^3$$$.
For each test case, print a single integer — the maximum possible value of the above expression among all rearrangements $$$t$$$ of $$$s$$$.
54abcd5abcba3aaa12adhocsmatter7abccabc
2 5 3 7 5
There are $$$n$$$ piles of stones. Two players take turns. In his turn, the player must do the following:
Choose exactly two different non-empty piles, and take from each of them exactly one stone.
The player who cannot make a move loses. Who will win if the game is played correctly?
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Then follows a description of the test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 5$$$) — the number of piles. Note the small bound on $$$n$$$!
The second line of each input contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the numbers of stones in the piles.
For each set of input data, if the first player (the player who starts) wins, print First. Otherwise, print Second.
522 223 531 42 3342023 2023 2023 2023510 10 10 10 11
Second First First Second First
You are given a sequence of $$$n$$$ non-negative integers $$$a$$$. You can perform the following operation on it.
Pick a pair of indices $$$l, r$$$ $$$(1 \le l \le r \le n)$$$ such that $$$a_l \oplus a_{l+1} \oplus \dots a_r \neq 0$$$ and reverse the subarray $$$a_l, a_{l+1}, \dots a_r$$$. Here, $$$\oplus$$$ represents the bitwise XOR operation.
Given $$$b$$$ which is a permutation of $$$a$$$, find a sequence of at most $$$2n$$$ such operations that transform $$$a$$$ into $$$b$$$. It can be shown that such a sequence always exists.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of each test case follows.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^3$$$) — the number of integers in $$$a$$$.
The next two lines contain $$$n$$$ space separated integers each, the elements of $$$a$$$ and $$$b$$$ respectively ($$$0 \le a_i, b_i \le 10^9$$$). It is guaranteed that $$$b$$$ is a permutation of $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^3$$$.
For each test case, print $$$k$$$ ($$$0 \le k \le 2 \cdot n$$$) — the number of operations to be performed on $$$a$$$ followed by $$$k$$$ lines containing integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \le l_i \le r_i \le n)$$$, indicating that in the $$$i^\text{th}$$$ operation the subarray $$$[l_i, r_i]$$$ of $$$a$$$ is to be reversed.
331 2 12 1 151 5 2 4 32 3 5 1 443 2 7 37 3 3 2
1 1 2 3 2 4 1 3 2 5 2 2 4 1 3
A multiset $$$S$$$ is said to be $$$n$$$-admissible if there exists some tournament graph on $$$n$$$ vertices whose multiset of outdegrees is $$$S$$$.
An $$$n$$$-admissible multiset $$$S$$$ is good if there exists a unique tournament graph on $$$n$$$ vertices (up to isomorphism) whose multiset of outdegrees equals $$$S$$$.
Find the number of good $$$n$$$-admissible multisets with all elements being even integers.
The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$ — the number of test cases.
The only line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 10^{18})$$$.
For each test case, print a single integer on a new line, the number of good $$$n$$$-admissible multisets with all elements being even integers, modulo $$$10^9 + 7$$$.
54638121
1 0 0 2 2178309
A tournament graph is a directed graph with no self-loops such that for each pair of vertices $$$u \ne v$$$, exactly one of the edges $$$u \to v$$$ and $$$v \to u$$$ exists.