#P8493. [IOI 2022] 数字电路

[IOI 2022] 数字电路

Background

Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.

This is an interactive problem. You only need to implement the functions required in the code.

Your code does not need to include any extra header files, and you do not need to implement the main function.

Since this problem has too many test points, considering the current Luogu judging implementation, this problem will not be scored by the subtasks given in the statement.

Problem Description

There is a digital circuit consisting of N+MN + M gates numbered from 00 to N+M1N + M - 1. Gates 00 to N1N - 1 are threshold gates, and gates NN to N+M1N + M - 1 are input gates.

Every gate except gate 00 is an input of exactly one threshold gate. Specifically, for each ii with 1iN+M11 \le i \le N + M - 1, gate ii is an input of gate P[i]P[i], where 0P[i]N10 \le P[i] \le N - 1. Importantly, it is guaranteed that P[i]<iP[i] \lt i. Also, assume P[0]=1P[0] = -1. Each threshold gate has one or more inputs. Input gates have no inputs.

Each gate has a state, either 00 or 11. The initial states of input gates are given by an array AA of MM integers. That is, for each jj with 0jM10 \le j \le M - 1, the initial state of input gate N+jN + j is A[j]A[j].

The state of each threshold gate depends on the states of its inputs as follows. First, each threshold gate is assigned a threshold parameter. For a threshold gate with cc inputs, the assigned parameter must be an integer between 11 and cc (inclusive). Then, for a threshold gate with parameter pp, if at least pp of its input gates have state 11, the threshold gate’s current state is 11; otherwise, its state is 00.

For example, suppose N=3N = 3 threshold gates and M=4M = 4 input gates. Gate 00 has inputs gates 11 and 66, gate 11 has inputs gates 22, 44, and 55, and gate 22 has only one input gate 33.

An illustration of this example is shown in the figure below.

Assume input gates 33 and 55 have state 11, while gates 44 and 66 have state 00. Suppose threshold gates 22, 11, 00 are assigned parameters 11, 22, 22, respectively. In this case, gate 22 has state 11, gate 11 has state 11, and gate 00 has state 00. The following figure shows the parameter assignment and the resulting states. Gates with state 11 are colored black.

The states of input gates will go through QQ updates. Each update is described by two integers LL and RR (NLRN+M1N \le L \le R \le N + M - 1), meaning to flip the states of all input gates with indices between LL and RR (inclusive). That is, for all ii with LiRL \le i \le R, if input gate ii has state 00 it will be flipped to 11; if it has state 11 it will be flipped to 00. After being flipped, each gate stays in the new state until it is flipped again in some later update.

Your goal is to compute, after each update, how many assignments of threshold-gate parameters make gate 00 have state 11. Two parameter assignments are considered different if at least one threshold gate has a different parameter. Since the number of assignments can be large, you need to output it modulo 1  000  002  0221\;000\;002\;022.

Note that in the example above, there are 66 different assignments of threshold-gate parameters in total, because gates 00, 11, 22 have 22, 33, 11 inputs, respectively. Among these 66 assignments, there are 22 assignments that make gate 00 have state 11.

Input Format

Your task is to implement the following two functions.

void init(int N, int M, int[] P, int[] A)
  • NN: the number of threshold gates.
  • MM: the number of input gates.
  • PP: an array of length N+MN + M, describing the inputs of threshold gates.
  • AA: an array of length MM, giving the initial states of input gates.
  • This function is called exactly once, and it is called before any calls to count_ways.
int count_ways(int L, int R)
  • LL, RR: the states of input gates with indices between LL and RR will be flipped.
  • This function should first perform the required update, then return the number of parameter assignments that make gate 00 have state 11, modulo 1  000  002  0221\;000\;002\;022.
  • This function will be called exactly QQ times.

Output Format

Consider the following sequence of function calls:

init(3, 4, [-1, 0, 1, 2, 1, 1, 0], [1, 0, 1, 0])

The statement has already explained this example.

count_ways(3, 4)

This call flips the states of gates 33 and 44, meaning gate 33 becomes 00 and gate 44 becomes 11. The following shows two valid parameter assignments that can make gate 00 have state 11.

Assignment 11 Assignment 22

In all other parameter assignments, gate 00 has state 00. Therefore, the function should return 22.

count_ways(4, 5)

This call flips the states of gates 44 and 55. As a result, all input gates have state 00, and for every parameter assignment, gate 00 has state 00. Therefore, the function should return 00.

count_ways(3, 6)

This call sets all input gates to state 11. As a result, for every parameter assignment, gate 00 has state 11. Therefore, the function should return 66.

Hint

Constraints

  • 1N,M1051 \le N, M \le 10^5.
  • 1Q1051 \le Q \le 10^5.
  • P[0]=1P[0] = -1.
  • 0P[i]<i0 \le P[i] \lt i and P[i]N1P[i] \le N - 1 (for all ii with 1iN+M11 \le i \le N + M - 1).
  • Each threshold gate has at least one input (for every ii with 0iN10 \le i \le N - 1, there exists an index xx such that i<xN+M1i \lt x \le N + M - 1 and P[x]=iP[x] = i).
  • 0A[j]10 \le A[j] \le 1 (for all jj with 0jM10 \le j \le M - 1).
  • NLRN+M1N \le L \le R \le N + M - 1.

Subtasks

  1. (2 points) N=1N = 1, M1000M \le 1000, Q5Q \le 5.
  2. (7 points) N,M1000N, M \le 1000, Q5Q \le 5, each threshold gate has exactly two inputs.
  3. (9 points) N,M1000N, M \le 1000, Q5Q \le 5.
  4. (4 points) M=N+1M = N + 1, M=2zM = 2^z (for some positive integer zz), P[i]=i12P[i] = \lfloor\frac{i - 1}{2}\rfloor (for all ii with 1iN+M11 \le i \le N + M - 1), L=RL = R.
  5. (12 points) M=N+1M = N + 1, M=2zM = 2^z (for some positive integer zz), P[i]=i12P[i] = \lfloor\frac{i - 1}{2}\rfloor (for all ii with 1iN+M11 \le i \le N + M - 1).
  6. (27 points) Each threshold gate has exactly two inputs.
  7. (28 points) N,M5000N, M \le 5000.
  8. (11 points) No additional constraints.

Sample Grader

The sample grader reads input in the following format:

  • Line 11: N  M  QN \; M \; Q.
  • Line 22: P[0]  P[1]    P[N+M1]P[0] \; P[1] \; \ldots \; P[N + M - 1].
  • Line 33: A[0]  A[1]    A[M1]A[0] \; A[1] \; \ldots \; A[M - 1].
  • Line 4+k4 + k (0kQ10 \le k \le Q - 1): L  RL \; R for the kk-th update.

The sample grader prints your answers in the following format:

  • Line 1+k1 + k (0kQ10 \le k \le Q - 1): the return value of count_ways for the kk-th update.

Conventions

When giving function interfaces, the statement uses generic type names void, bool, int, int[] (array), and union(bool, int[]).

In C++, the grader will use suitable data types or implementations as shown in the table below:

void bool int int[]
void bool int std::vector<int>
union(bool, int[]) Length of array a
std::variant<bool, std::vector<int>> a.size()

In C++, std::variant is defined in the header <variant>. A function with return type std::variant<bool, std::vector<int>> can return either a bool or a std::vector<int>. The following sample code shows three functions that return std::variant, and all of them work correctly:

std::variant<bool, std::vector<int>> foo(int N) {
    return N % 2 == 0;
}

std::variant<bool, std::vector<int>> goo(int N) {
    return std::vector<int>(N, 0);
}

std::variant<bool, std::vector<int>> hoo(int N) {
    if (N % 2 == 0) {
        return false;
    }

    return std::vector<int>(N, 0);
}

Translated by ChatGPT 5