#P16067. [CSPro 32] 宝藏

[CSPro 32] 宝藏

Background

The testdata on Luogu are only for non-official community use and are not official testdata. Official judging link: https://www.cspro.org/.

Problem Description

On Xixi-Aifu Island, a treasure is buried. Little C found the treasure’s location according to a treasure map. The treasure chest is locked, and there are some hints written beside it:

  • You are given nn instructions, numbered 1∼n1 \sim n. Each instruction is an operation on a deque, and all elements in the deque are 2×22 \times 2 matrices.
  • At certain times, some instruction may be modified.
  • At certain times, the password can be computed as follows: for a given instruction interval [l,r][l, r], starting from an empty deque, execute instructions l∼rl \sim r in order. Multiply all matrices in the resulting deque from front to back, and take every entry of the product matrix modulo 998244353998244353. The resulting matrix is the password. In particular, if the deque is empty, the password is the identity matrix. If you can compute the passwords at these times, you will be able to open the chest and obtain the treasure.

After observing, Little C found that each instruction is always one of the following three forms:

  1. Given a 2×22 \times 2 matrix A\mathbf{A}, insert A\mathbf{A} at the front of the deque.
  2. Given a 2×22 \times 2 matrix B\mathbf{B}, insert B\mathbf{B} at the back of the deque.
  3. If the deque is not empty, delete the matrix that was inserted most recently.

Little C recorded all events that happened over time. Specifically, there are mm time points, and at each time point one of the following two types of events may occur:

  1. Instruction ii changes; the modified instruction is still one of the three forms above.
  2. Given an instruction interval [l,r][l, r], compute the password obtained by executing instructions l∼rl \sim r in order.

Since Little C does not know how to solve this problem, he asks you for help. You need to output the password for every event of type 2.

Input Format

Read input from standard input.

The first line contains two positive integers n,mn, m.

The next nn lines give the instructions at the initial time in order:

  • The first integer vv describes the form of the instruction, and vv is guaranteed to be one of 1,2,31, 2, 3.
  • If v=1v = 1, then four non-negative integers A1,1,A1,2,A2,1,A2,2A_{1,1}, A_{1,2}, A_{2,1}, A_{2,2} follow, meaning the operation is to insert the 2×22 \times 2 matrix A\mathbf{A} at the front of the deque.
  • If v=2v = 2, then four non-negative integers B1,1,B1,2,B2,1,B2,2B_{1,1}, B_{1,2}, B_{2,1}, B_{2,2} follow, meaning the operation is to insert the 2×22 \times 2 matrix B\mathbf{B} at the back of the deque.
  • If v=3v = 3, it means: if the deque is not empty, delete the matrix that was inserted most recently.

The next mm lines describe the events at each time point in order:

  • The first integer vv describes the type of the event, and vv is guaranteed to be one of 1,21, 2.
  • If v=1v = 1, then a positive integer ii and an instruction follow, meaning to update instruction ii to the given instruction. The input format of the instruction is the same as in the initial instructions.
  • If v=2v = 2, then two positive integers l,rl, r follow; you need to compute the password obtained by executing instructions l∼rl \sim r in order.

Output Format

Write output to standard output.

For every event of type 22, output one line with four non-negative integers C1,1,C1,2,C2,1,C2,2C_{1,1}, C_{1,2}, C_{2,1}, C_{2,2}, representing the password matrix C\mathbf{C} at that time.

3 4
1 2 3 9 3
2 6 9 4 2
2 2 8 2 1
2 2 3
1 2 1 3 1 0 1
1 3 3
2 1 3
30 57 12 34
2 3 9 3

Hint

Explanation for Sample 1

When the first event happens:

  • Instruction 22 inserts the matrix [6942]\begin{bmatrix} 6 & 9 \\ 4 & 2 \end{bmatrix} at the back of the sequence.
  • Instruction 33 inserts the matrix [2821]\begin{bmatrix} 2 & 8 \\ 2 & 1 \end{bmatrix} at the back of the sequence.

Executing instructions 2∼32 \sim 3 in order, the resulting deque is $\begin{bmatrix} 6 & 9 \\ 4 & 2 \end{bmatrix}, \begin{bmatrix} 2 & 8 \\ 2 & 1 \end{bmatrix}$, so the password is

$$\begin{bmatrix} 6 & 9 \\ 4 & 2 \end{bmatrix} \times \begin{bmatrix} 2 & 8 \\ 2 & 1 \end{bmatrix} = \begin{bmatrix} 30 & 57 \\ 12 & 34 \end{bmatrix}$$

When the fourth event happens:

  • Instruction 11 inserts the matrix [2393]\begin{bmatrix} 2 & 3 \\ 9 & 3 \end{bmatrix} at the front of the sequence.
  • Instruction 22 inserts the matrix [3101]\begin{bmatrix} 3 & 1 \\ 0 & 1 \end{bmatrix} at the front of the sequence.
  • Instruction 33 means: if the deque is not empty, delete the matrix that was inserted most recently.

Executing instructions 1∼31 \sim 3 in order, the resulting deque is [2393]\begin{bmatrix} 2 & 3 \\ 9 & 3 \end{bmatrix}, so the password is [2393]\begin{bmatrix} 2 & 3 \\ 9 & 3 \end{bmatrix}.

Sample 2

See 2.in and 2.ans under the problem directory.

This sample satisfies the constraints of testdata 1∼31 \sim 3.

Sample 3

See 3.in and 3.ans under the problem directory.

This sample satisfies the constraints of testdata 4∼74 \sim 7.

Sample 4

See 4.in and 4.ans under the problem directory.

This sample satisfies the constraints of testdata 8,98, 9.

Sample 5

See 5.in and 5.ans under the problem directory.

This sample satisfies the constraints of testdata 10,1110, 11.

Sample 6

See 6.in and 6.ans under the problem directory.

This sample satisfies the constraints of testdata 12∼1512 \sim 15.

Sample 7

See 7.in and 7.ans under the problem directory.

This sample satisfies the constraints of testdata 16,1716, 17.

Subtasks

For all testdata, it holds that 1≤n,m≤1051 \le n, m \le 10^5, 0≤Ai,j,Bi,j<9982443530 \le A_{i,j}, B_{i,j} < 998244353, and 1≤l≤r≤n1 \le l \le r \le n.

Test Point ID n,m≤n, m \le Special Property
1∼31 \sim 3 10210^2 None
4∼74 \sim 7 10310^3 ^
8,98, 9 5×1045 \times 10^4 All instructions are of form 11
10,1110, 11 ^ All instructions are of form 11 or 22
12∼1512 \sim 15 All events are of type 22
16,1716, 17 None
18∼2018 \sim 20 10510^5 ^

Translated by ChatGPT 5