There is a circuit, which consists of $$$N + M$$$ gates numbered from $$$0$$$ to $$$N + M - 1$$$. Gates $$$0$$$ to $$$N - 1$$$ are threshold gates, whereas gates $$$N$$$ to $$$N + M - 1$$$ are source gates.
Each gate, except for gate $$$0$$$, is an input to exactly one threshold gate. Specifically, for each $$$i$$$ such that $$$1 \le i \le N + M - 1$$$, gate $$$i$$$ is an input to gate $$$P[i]$$$, where $$$0 \le P[i] \le N-1$$$. Importantly, we also have $$$P[i] \textless i$$$. Moreover, we assume $$$P[0] = -1$$$. Each threshold gate has one or more inputs. Source gates do not have any inputs.
Each gate has a state which is either $$$0$$$ or $$$1$$$. The initial states of the source gates are given by an array $$$A$$$ of $$$M$$$ integers. That is, for each $$$j$$$ such that $$$0 \le j \le M - 1$$$, the initial state of the source gate $$$N + j$$$ is $$$A[j]$$$.
The state of each threshold gate depends on the states of its inputs and is determined as follows. First, each threshold gate is assigned a threshold parameter. The parameter assigned to a threshold gate with $$$c$$$ inputs must be an integer between $$$1$$$ and $$$c$$$ (inclusive). Then, the state of a threshold gate with parameter $$$p$$$ is $$$1$$$, if at least $$$p$$$ of its inputs have state $$$1$$$, and $$$0$$$ otherwise.
For example, suppose there are $$$N = 3$$$ threshold gates and $$$M = 4$$$ source gates. The inputs to gate $$$0$$$ are gates $$$1$$$ and $$$6$$$, the inputs to gate $$$1$$$ are gates $$$2$$$, $$$4$$$, and $$$5$$$, and the only input to gate $$$2$$$ is gate $$$3$$$.
This example is illustrated in the following picture.

Suppose that source gates $$$3$$$ and $$$5$$$ have state $$$1$$$, while source gates $$$4$$$ and $$$6$$$ have state $$$0$$$. Assume we assign parameters $$$1$$$, $$$2$$$ and $$$2$$$ to threshold gates $$$2$$$, $$$1$$$ and $$$0$$$ respectively. In this case, gate $$$2$$$ has state $$$1$$$, gate $$$1$$$ has state $$$1$$$ and gate $$$0$$$ has state $$$0$$$. This assignment of parameter values and the states is illustrated in the following picture. Gates whose state is $$$1$$$ are marked in black.

The states of the source gates will undergo $$$Q$$$ updates. Each update is described by two integers $$$L$$$ and $$$R$$$ ($$$N \le L \le R \le N + M - 1$$$) and toggles the states of all source gates numbered between $$$L$$$ and $$$R$$$, inclusive. That is, for each $$$i$$$ such that $$$L \le i \le R$$$, source gate $$$i$$$ changes its state to $$$1$$$, if its state is $$$0$$$, or to $$$0$$$, if its state is $$$1$$$. The new state of each toggled gate remains unchanged until it is possibly toggled by one of the later updates.
Your goal is to count, after each update, how many different assignments of parameters to threshold gates result in gate $$$0$$$ having state $$$1$$$. Two assignments are considered different if there exists at least one threshold gate that has a different value of its parameter in both assignments. As the number of ways can be large, you should compute it modulo $$$1\;000\;002\;022$$$.
Note that in the example above, there are $$$6$$$ different assignments of parameters to threshold gates, since gates $$$0$$$, $$$1$$$ and $$$2$$$ have $$$2$$$, $$$3$$$ and $$$1$$$ inputs respectively. In $$$2$$$ out of these $$$6$$$ assignments, gate $$$0$$$ has state $$$1$$$.
Implementation Details
Your task is to implement two procedures.
The sample grader reads the input in the following format:
The sample grader prints your answers in the following format:
| Subtask | Points | Additional Input Constraints |
| 1 | 2 | $$$N = 1$$$, $$$M \le 1000$$$, $$$Q \le 5$$$ |
| 2 | 7 | $$$N, M \le 1000$$$, $$$Q \le 5$$$, each threshold gate has exactly two inputs. |
| 3 | 9 | $$$N, M \le 1000$$$, $$$Q \le 5$$$ |
| 4 | 4 | $$$M = N + 1$$$, $$$M = 2^z$$$ (for some positive integer $$$z$$$), $$$P[i] = \lfloor\frac{i - 1}{2}\rfloor$$$ (for each $$$i$$$ such that $$$1 \le i \le N + M - 1$$$), $$$L = R$$$ |
| 5 | 12 | $$$M = N + 1$$$, $$$M = 2^z$$$ (for some positive integer $$$z$$$), $$$P[i] = \lfloor\frac{i - 1}{2}\rfloor$$$ (for each $$$i$$$ such that $$$1 \le i \le N + M - 1$$$) |
| 6 | 27 | Each threshold gate has exactly two inputs. |
| 7 | 28 | $$$N, M \le 5000$$$ |
| 8 | 11 | No additional constraints |
3 4 3 -1 0 1 2 1 1 0 1 0 1 0 3 4 4 5 3 6
2 0 6
Consider the following sequence of calls:
init(3, 4, [-1, 0, 1, 2, 1, 1, 0], [1, 0, 1, 0])
This example is illustrated in the task description above.
count_ways(3, 4)
This toggles the states of gates $$$3$$$ and $$$4$$$, i.e. the state of gate $$$3$$$ becomes $$$0$$$, and the state of gate $$$4$$$ becomes $$$1$$$. Two ways of assigning the parameters which result in gate $$$0$$$ having state $$$1$$$ are illustrated in the pictures below.
| Way $$$1$$$ | Way $$$2$$$ |
![]() | ![]() |
In all other assignments of parameters, gate $$$0$$$ has state $$$0$$$. Thus, the procedure should return $$$2$$$.
count_ways(4, 5)
This toggles the states of gates $$$4$$$ and $$$5$$$. As a result, all source gates have state $$$0$$$, and for any assignment of parameters, gate $$$0$$$ has state $$$0$$$. Thus, the procedure should return $$$0$$$.
count_ways(3, 6)
This changes the states of all source gates to $$$1$$$. As a result, for any assignment of parameters, gate $$$0$$$ has state $$$1$$$. Thus, the procedure should return $$$6$$$.
There are $$$N$$$ insects, indexed from $$$0$$$ to $$$N - 1$$$, running around Pak Blangkon's house. Each insect has a type, which is an integer between $$$0$$$ and $$$10^9$$$ inclusive. Multiple insects may have the same type.
Suppose insects are grouped by type. We define the cardinality of the most frequent insect type as the number of insects in a group with the most number of insects. Similarly, the cardinality of the rarest insect type is the number of insects in a group with the least number of insects.
For example, suppose that there are $$$11$$$ insects, whose types are $$$[5, 7, 9, 11, 11, 5, 0, 11, 9, 100, 9]$$$. In this case, the cardinality of the most frequent insect type is $$$3$$$. The groups with the most number of insects are type $$$9$$$ and type $$$11$$$, each consisting of $$$3$$$ insects. The cardinality of the rarest insect type is $$$1$$$. The groups with the least number of insects are type $$$7$$$, type $$$0$$$, and type $$$100$$$, each consisting of $$$1$$$ insect.
Pak Blangkon does not know the type of any insect. He has a machine with a single button that can provide some information about the types of the insects. Initially, the machine is empty. To use the machine, three types of operations can be performed:
Whenever the button is pressed, the machine reports the cardinality of the most frequent insect type, considering only insects inside the machine.
Your task is to determine the cardinality of the rarest insect type among all $$$N$$$ insects in Pak Blangkon's house by using the machine. Additionally, in some subtasks, your score depends on the maximum number of operations of a given type that are performed (see Subtasks section for details).
Implementation Details
You should implement the following procedure:
Let $$$T$$$ be an array of $$$N$$$ integers where $$$T[i]$$$ is the type of insect $$$i$$$.
The sample grader reads the input in the following format:
If the sample grader detects a protocol violation, the output of the sample grader is Protocol Violation: <MSG>, where <MSG> is one of the following:
Otherwise, the output of the sample grader is in the following format:
| Subtask | Points | Additional Input Constraints |
| 1 | 10 | $$$N \le 200$$$ |
| 2 | 15 | $$$N \le 1000$$$ |
| 3 | 75 | No additional constraints |
If in any of the test cases, the calls to the procedures move_inside, move_outside, or press_button do not conform to the constraints described in Implementation Details, or the return value of min_cardinality is incorrect, the score of your solution for that subtask will be $$$0$$$.
Let $$$q$$$ be the maximum of the following three values: the number of calls to move_inside, the number of calls to move_outside, and the number of calls to press_button.
In subtask 3, you can obtain a partial score. Let $$$m$$$ be the maximum value of $$$\frac{q}{N}$$$ across all test cases in this subtask. Your score for this subtask is calculated according to the following table:
| Condition | Points | ||
| $$$20 \textless m$$$ | $$$0$$$ (reported as | quot;Output isn't correct | quot; in CMS) |
| $$$6 \textless m \le 20$$$ | $$$\frac{225}{m - 2}$$$ | ||
| $$$3 \textless m \le 6$$$ | $$$81 - \frac{2}{3} m^2$$$ | ||
| $$$m \le 3$$$ | $$$75$$$ |
Consider a scenario in which there are $$$6$$$ insects of types $$$[5, 8, 9, 5, 9, 9]$$$ respectively. The procedure min_cardinality is called in the following way:
min_cardinality(6)
The procedure may call move_inside, move_outside, and press_button as follows.
| Call | Return value | Insects in the machine | Types of insects in the machine |
| $$$\{\}$$$ | $$$[]$$$ | ||
| move_inside(0) | $$$\{0\}$$$ | $$$[5]$$$ | |
| press_button() | $$$1$$$ | $$$\{0\}$$$ | $$$[5]$$$ |
| move_inside(1) | $$$\{0, 1\}$$$ | $$$[5, 8]$$$ | |
| press_button() | $$$1$$$ | $$$\{0, 1\}$$$ | $$$[5, 8]$$$ |
| move_inside(3) | $$$\{0, 1, 3\}$$$ | $$$[5, 8, 5]$$$ | |
| press_button() | $$$2$$$ | $$$\{0, 1, 3\}$$$ | $$$[5, 8, 5]$$$ |
| move_inside(2) | $$$\{0, 1, 2, 3\}$$$ | $$$[5, 8, 9, 5]$$$ | |
| move_inside(4) | $$$\{0, 1, 2, 3, 4\}$$$ | $$$[5, 8, 9, 5, 9]$$$ | |
| move_inside(5) | $$$\{0, 1, 2, 3, 4, 5\}$$$ | $$$[5, 8, 9, 5, 9, 9]$$$ | |
| press_button() | $$$3$$$ | $$$\{0, 1, 2, 3, 4, 5\}$$$ | $$$[5, 8, 9, 5, 9, 9]$$$ |
| move_inside(5) | $$$\{0, 1, 2, 3, 4, 5\}$$$ | $$$[5, 8, 9, 5, 9, 9]$$$ | |
| press_button() | $$$3$$$ | $$$\{0, 1, 2, 3, 4, 5\}$$$ | $$$[5, 8, 9, 5, 9, 9]$$$ |
| move_outside(5) | $$$\{0, 1, 2, 3, 4\}$$$ | $$$[5, 8, 9, 5, 9]$$$ | |
| press_button() | $$$2$$$ | $$$\{0, 1, 2, 3, 4\}$$$ | $$$[5, 8, 9, 5, 9]$$$ |
At this point, there is sufficient information to conclude that the cardinality of the rarest insect type is $$$1$$$. Therefore, the procedure min_cardinality should return $$$1$$$.
In this example, move_inside is called $$$7$$$ times, move_outside is called $$$1$$$ time, and press_button is called $$$6$$$ times.
Thousands Islands is a group of beautiful islands located in the Java Sea. It consists of $$$N$$$ islands, numbered from $$$0$$$ to $$$N - 1$$$.
There are $$$M$$$ canoes, numbered from $$$0$$$ to $$$M - 1$$$, that can be used to sail between islands. For each $$$i$$$ such that $$$0 \le i \le M - 1$$$, canoe $$$i$$$ can be docked either at island $$$U[i]$$$ or $$$V[i]$$$, and can be used to sail between islands $$$U[i]$$$ and $$$V[i]$$$. Specifically, when the canoe is docked at island $$$U[i]$$$, it can be used to sail from island $$$U[i]$$$ to island $$$V[i]$$$, after which the canoe becomes docked at island $$$V[i]$$$. Similarly, when the canoe is docked at island $$$V[i]$$$, it can be used to sail from island $$$V[i]$$$ to island $$$U[i]$$$, after which the canoe becomes docked at island $$$U[i]$$$. Initially, the canoe is docked at island $$$U[i]$$$. It is possible that multiple canoes can be used to sail between the same pair of islands. It is also possible that multiple canoes are docked at the same island.
For safety reasons, a canoe needs to be maintained after every time it is sailed, which forbids the same canoe to be sailed two times in a row. That is, after using some canoe $$$i$$$, another canoe must be used before canoe $$$i$$$ can be used again.
Bu Dengklek wants to plan a journey through some of the islands. Her journey is valid if and only if the following conditions are satisfied.
Implementation Details
You should implement the following procedure:
union(bool, int[]) find_journey(int N, int M, int[] U, int[] V)
The sample grader reads the input in the following format:
The sample grader prints your answers in the following format:
| Subtask | Points | Additional Input Constraints |
| 1 | 5 | $$$N = 2$$$ |
| 2 | 5 | $$$N \le 400$$$. For each pair of distinct islands $$$x$$$ and $$$y$$$ ($$$0 \le x \textless y \le N - 1$$$), there are exactly two canoes that can be used to sail between them. One of them is docked at island $$$x$$$, and the other one is docked at island $$$y$$$. |
| 3 | 21 | $$$N \le 1000$$$, $$$M$$$ is even, and for each even $$$i$$$ such that $$$0 \le i \le M - 1$$$, canoes $$$i$$$ and $$$i + 1$$$ can both be used to sail between islands $$$U[i]$$$ and $$$V[i]$$$. Canoe $$$i$$$ is initially docked at island $$$U[i]$$$ and canoe $$$i + 1$$$ is initially docked at island $$$V[i]$$$. Formally, $$$U[i] = V[i + 1]$$$ and $$$V[i] = U[i + 1]$$$. |
| 4 | 24 | $$$N \le 1000$$$, $$$M$$$ is even, and for each even $$$i$$$ such that $$$0 \le i \le M - 1$$$, canoes $$$i$$$ and $$$i + 1$$$ can both be used to sail between islands $$$U[i]$$$ and $$$V[i]$$$. Both canoes are initially docked at island $$$U[i]$$$. Formally, $$$U[i] = U[i + 1]$$$ and $$$V[i] = V[i + 1]$$$. |
| 5 | 45 | No additional constraints |
For each test case in which a valid journey exists, your solution:
Example 1
Consider the following call:
find_journey(4, 5, [0, 1, 2, 0, 3], [1, 2, 3, 3, 1])
The islands and canoes are shown in the picture below.

One possible valid journey is as follows. Bu Dengklek first sails canoes $$$0$$$, $$$1$$$, $$$2$$$, and $$$4$$$ in that order. As a result, she is at island $$$1$$$. After that, Bu Dengklek can sail canoe $$$0$$$ again as it is currently docked at island $$$1$$$ and the last canoe she used is not canoe $$$0$$$. After sailing canoe $$$0$$$ again, Bu Dengklek is now at island $$$0$$$. However, canoes $$$1$$$, $$$2$$$ and $$$4$$$ are not docked at the same islands as they were before the journey. Bu Dengklek then continues her journey by sailing canoes $$$3$$$, $$$2$$$, $$$1$$$, $$$4$$$, and $$$3$$$ again. Bu Dengklek is back at island $$$0$$$ and all the canoes are docked at the same islands as before the journey.
Therefore, the returned value $$$[0, 1, 2, 4, 0, 3, 2, 1, 4, 3]$$$ represents a valid journey.
Example 2
Consider the following call:
find_journey(2, 3, [0, 1, 1], [1, 0, 0])
The islands and canoes are shown in the picture below.

Bu Dengklek can only start by sailing canoe $$$0$$$, after which she can sail either canoe $$$1$$$ or $$$2$$$. Note that she cannot sail canoe $$$0$$$ twice in a row. In both cases, Bu Dengklek is back at island $$$0$$$. However, the canoes are not docked at the same islands as they were before the journey, and Bu Dengklek cannot sail any canoe afterwards, as the only canoe docked at island $$$0$$$ is the one she has just used. As there is no valid journey, the procedure should return false.