IOI 2022 day 2
D. Digital Circuit
time limit per test
2 seconds
memory limit per test
2048 MB
input
standard input
output
standard output

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.

  • void init(int N, int M, int[] P, int[] A)
    • $$$N$$$: the number of threshold gates.
    • $$$M$$$: the number of source gates.
    • $$$P$$$: an array of length $$$N + M$$$ describing the inputs to the threshold gates.
    • $$$A$$$: an array of length $$$M$$$ describing the initial states of the source gates.
    • This procedure is called exactly once, before any calls to count_ways.
  • int count_ways(int L, int R)
    • $$$L$$$, $$$R$$$: the boundaries of the range of source gates, whose states are toggled.
    • This procedure should first perform the specified update, and then return the number of ways, modulo $$$1\;000\;002\;022$$$, of assigning parameters to the threshold gates, which result in gate $$$0$$$ having state $$$1$$$.
    • This procedure is called exactly $$$Q$$$ times.
Input

The sample grader reads the input in the following format:

  • line $$$1$$$: $$$N \; M \; Q$$$ ($$$1 \le N, M \le 100\;000$$$, $$$1 \le Q \le 100\;000$$$)
  • line $$$2$$$: $$$P[0] \; P[1] \; \ldots \; P[N + M - 1]$$$ ($$$P[0] = -1$$$, $$$0 \le P[i] \textless i$$$ and $$$P[i] \le N - 1$$$ (for each $$$i$$$ such that $$$1 \le i \le N + M - 1$$$))
  • line $$$3$$$: $$$A[0] \; A[1] \; \ldots \; A[M - 1]$$$ ($$$0 \le A[j] \le 1$$$)
  • line $$$4 + k$$$ ($$$0 \le k \le Q - 1$$$): $$$L \; R$$$ for update $$$k$$$ ($$$N \le L \le R \le N + M - 1$$$)
Each threshold gate has at least one input (for each $$$i$$$ such that $$$0 \le i \le N - 1$$$ there exists an index $$$x$$$ such that $$$i \textless x \le N + M - 1$$$ and $$$P[x] = i$$$).
Output

The sample grader prints your answers in the following format:

  • line $$$1 + k$$$ ($$$0 \le k \le Q - 1$$$): the return value of count_ways for update $$$k$$$
Scoring
SubtaskPoints Additional Input Constraints
12$$$N = 1$$$, $$$M \le 1000$$$, $$$Q \le 5$$$
27 $$$N, M \le 1000$$$, $$$Q \le 5$$$, each threshold gate has exactly two inputs.
39$$$N, M \le 1000$$$, $$$Q \le 5$$$
44 $$$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$$$
512 $$$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$$$)
627Each threshold gate has exactly two inputs.
728$$$N, M \le 5000$$$
811No additional constraints
Example
Input
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
Output
2
0
6
Note

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

E. Rarest Insects
time limit per test
2 seconds
memory limit per test
2048 MB
input
standard input
output
standard output

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:

  1. Move an insect to inside the machine.
  2. Move an insect to outside the machine.
  3. Press the button on the machine.
Each type of operation can be performed at most $$$40\;000$$$ times.

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:

  • int min_cardinality(int N)
    • $$$N$$$: the number of insects.
    • This procedure should return the cardinality of the rarest insect type among all $$$N$$$ insects in Pak Blangkon's house.
    • This procedure is called exactly once.
    The above procedure can make calls to the following procedures:
  • void move_inside(int i)
    • $$$i$$$: the index of the insect to be moved inside the machine. The value of $$$i$$$ must be between $$$0$$$ and $$$N - 1$$$ inclusive.
    • If this insect is already inside the machine, the call has no effect on the set of insects in the machine. However, it is still counted as a separate call.
    • This procedure can be called at most $$$40\;000$$$ times.
  • void move_outside(int i)
    • $$$i$$$: the index of the insect to be moved outside the machine. The value of $$$i$$$ must be between $$$0$$$ and $$$N - 1$$$ inclusive.
    • If this insect is already outside the machine, the call has no effect on the set of insects in the machine. However, it is still counted as a separate call.
    • This procedure can be called at most $$$40\;000$$$ times.
  • int press_button()
    • This procedure returns the cardinality of the most frequent insect type, considering only insects inside the machine.
    • This procedure can be called at most $$$40\;000$$$ times.
    • The grader is not adaptive. That is, the types of all $$$N$$$ insects are fixed before min_cardinality is called.
Interaction

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:

  • line $$$1$$$: $$$N$$$ ($$$2 \le N \le 2000$$$)
  • line $$$2$$$: $$$T[0] \; T[1] \; \ldots \; T[N - 1]$$$

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:

  • invalid parameter: in a call to move_inside or move_outside, the value of $$$i$$$ is not between $$$0$$$ and $$$N - 1$$$ inclusive.
  • too many calls: the number of calls to any of move_inside, move_outside, or press_button exceeds $$$40\;000$$$.

Otherwise, the output of the sample grader is in the following format:

  • line $$$1$$$: the return value of min_cardinality
  • line $$$2$$$: $$$q$$$
Scoring

SubtaskPoints Additional Input Constraints
110$$$N \le 200$$$
215$$$N \le 1000$$$
375No 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:

ConditionPoints
$$$20 \textless m$$$$$$0$$$ (reported asquot;Output isn't correctquot; 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$$$
Note

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.

CallReturn valueInsects in the machineTypes 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.

F. Thousands Islands
time limit per test
1 second
memory limit per test
2048 MB
input
standard input
output
standard output

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.

  • She starts and ends her journey at island $$$0$$$.
  • She visits at least one island other than island $$$0$$$.
  • After the journey ends, each canoe is docked at the same island as it was before the journey. I.e., canoe $$$i$$$, for each $$$i$$$ such that $$$0 \le i \le M - 1$$$, must be docked at island $$$U[i]$$$.
Help Bu Dengklek find any valid journey involving sailing at most $$$2\;000\;000$$$ times, or determine that no such valid journey exists. It can be proven that under the constraints specified in this task (see Constraints section), if a valid journey exists, there also exists a valid journey that does not involve sailing more than $$$2\;000\;000$$$ times.

Implementation Details

You should implement the following procedure:

union(bool, int[]) find_journey(int N, int M, int[] U, int[] V)

  • $$$N$$$: the number of islands.
  • $$$M$$$: the number of canoes.
  • $$$U$$$, $$$V$$$: arrays of length $$$M$$$ describing the canoes.
  • This procedure should return either a boolean or an array of integers.
    • If no valid journey exists, the procedure should return false.
    • If a valid journey exists, you have two options:
      • To be awarded the full score, the procedure should return an array of at most $$$2\;000\;000$$$ integers representing a valid journey. More precisely, the elements of this array should be the numbers of the canoes that are used in the journey (in the order they are used).
      • To be awarded a partial score, the procedure should return true, an array of more than $$$2\;000\;000$$$ integers, or an array of integers not describing a valid journey. (See the Subtasks section for more details.)
  • This procedure is called exactly once.
Input

The sample grader reads the input in the following format:

  • line $$$1$$$: $$$N \; M$$$ ($$$2 \le N \le 100\;000$$$, $$$1 \le M \le 200\;000$$$)
  • line $$$2 + i$$$ ($$$0 \le i \le M - 1$$$): $$$U[i] \; V[i]$$$ ($$$0 \le U[i] \le N - 1$$$ and $$$0 \le V[i] \le N - 1$$$, $$$U[i] \neq V[i]$$$)
Output

The sample grader prints your answers in the following format:

  • If find_journey returns a bool:
    • line $$$1$$$: $$$0$$$
    • line $$$2$$$: $$$0$$$ if find_journey returns false, or $$$1$$$ otherwise.
  • If find_journey returns an int[], denote the elements of this array by $$$c[0], c[1], \ldots c[k-1]$$$. The sample grader prints:
    • line $$$1$$$: $$$1$$$
    • line $$$2$$$: $$$k$$$
    • line $$$3$$$: $$$c[0] \; c[1] \; \ldots \; c[k-1]$$$
Scoring
SubtaskPoints Additional Input Constraints
15$$$N = 2$$$
25 $$$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$$$.
321 $$$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]$$$.
424 $$$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]$$$.
545No additional constraints

For each test case in which a valid journey exists, your solution:

  • gets full points if it returns a valid journey,
  • gets $$$35\%$$$ of the points if it returns true, an array of more than $$$2\;000\;000$$$ integers, or an array that does not describe a valid journey,
  • gets $$$0$$$ points otherwise.
For each test case in which a valid journey does not exist, your solution:
  • gets full points if it returns false,
  • gets $$$0$$$ points otherwise.
Note that the final score for each subtask is the minimum of the points for the test cases in the subtask.
Note

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.