#P2086. [NOI2012] 魔幻棋盘

    ID: 1026 远端评测题 5000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2012线段树NOI 系列最大公约数,gcd

[NOI2012] 魔幻棋盘

Description

Little Q, who is about to enter second grade, bought a new type of puzzle toy — the Magic Chessboard. It is a grid board with NN rows and MM columns, and each cell contains a positive integer. The board guardian stands at the XX-th row and the YY-th column (rows and columns are numbered starting from 11) and will never move. The board guardian performs two types of operations:

  • (a) Query: Based on his position, he expands in the four directions to form a rectangle of arbitrary size and asks you for the greatest common divisor of all numbers in this region.
  • (b) Modify: He arbitrarily selects a rectangular region on the board and adds a given integer to all numbers in this region.

The game manual includes this sentence: “Smart kids, when you answer 1993032419930324 consecutive queries correctly, you will get a surprise!” Little Q really wants this surprise, so he plays this toy every day. However, because he is careless, he often makes mistakes and cannot reach this goal. He now seeks your help, hoping you can write a program to answer the guardian’s queries with 100%100\% accuracy.

To simplify the problem, your program only needs to process TT operations from the board guardian, and it is guaranteed that at any time all numbers on the board are positive integers not exceeding 26212^{62} - 1.

Input Format

The first line contains two positive integers N,MN, M, representing the size of the board.

The second line contains two positive integers X,YX, Y, representing the position of the board guardian.

The third line contains a single positive integer TT, representing that the board guardian will perform TT operations.

The next NN lines each contain MM positive integers, describing the initial numbers at each position on the board.

The next TT lines, in chronological order, describe the TT operations. Each line starts with a digit 00 or 11:

  • If it starts with 00, the operation is a query, followed by four non-negative integers x1,y1,x2,y2x_1, y_1, x_2, y_2. The queried rectangle is obtained by expanding, based on the guardian’s position, x1x_1 rows upward, x2x_2 rows downward, y1y_1 columns to the left, and y2y_2 columns to the right (see the sample).
  • If it starts with 11, the operation is a modification, followed by four positive integers x1,y1,x2,y2x_1, y_1, x_2, y_2 and an integer cc. The upper and lower boundaries of the modified region are rows x1x_1 and x2x_2, and the left and right boundaries are columns y1y_1 and y2y_2 (see the sample). All numbers in this rectangle are increased by cc (note that cc may be negative).

Output Format

For each query operation, output one number per line, which is the greatest common divisor of all numbers in the specified region.

2 2
1 1
4
6 12
18 24
0 0 0 1 0
1 1 1 1 2 6
1 2 1 2 2 6
0 0 0 1 1
6
6

Hint

After the first and fourth operations (queries), the boldfaced part indicates the query region.

After the second and third operations (modifications), the boldfaced part indicates the modification region.

The testdata is divided into three categories A, B, and C:

  • Category A accounts for 20%20\%, satisfying N100N \leq 100, M100M \leq 100, T2×104T \leq 2\times 10^4.
  • Category B accounts for 40%40\%, satisfying N=1N = 1, M5×105M \leq 5\times 10^5, T105T \leq 10^5.
  • Category C accounts for 40%40\%, satisfying N×M5×105N \times M \leq 5\times 10^5, T105T \leq 10^5.

In each category, 50%50\% of the testdata satisfies that each modification affects only one cell (i.e., x1=x2x_1 = x_2, y1=y2y_1 = y_2).

The input data is guaranteed to satisfy all properties described in the statement.

Translated by ChatGPT 5