#P2086. Magical Chessboard: GCD Queries and Updates
Magical Chessboard: GCD Queries and Updates
Magical Chessboard: GCD Queries and Updates
In this problem, you are given an chessboard where each cell contains a positive integer. A board guardian is stationed at cell (1-indexed) and never moves. The guardian performs operations which are of two types:
- Query Operation: Starting from his fixed position, he randomly expands to form a rectangular query region (which always includes the guardian's cell) and asks for the greatest common divisor (GCD) of all numbers in that region. Formally, if the numbers in the region are , you are to compute .
- Update Operation: The guardian selects an arbitrary rectangular subregion of the chessboard and adds a given integer to every number within that region.
Your task is to process these operations. For each query operation, output the GCD of the specified region. It is guaranteed that at any moment, all numbers on the chessboard do not exceed .
inputFormat
The first line contains five integers , , , , and , where is the guardian's fixed position (1-indexed).
The next lines each contain positive integers representing the initial configuration of the chessboard.
Each of the following lines describes an operation in one of the following formats:
1 a b c d
: A query operation on the rectangular region with top-left corner and bottom-right corner (inclusive). It is guaranteed that and .2 a b c d v
: An update operation where the integer is added to every cell in the rectangular region with top-left corner and bottom-right corner (inclusive).
outputFormat
For each query operation, output a single line containing the greatest common divisor (GCD) of the numbers in the specified rectangular region.
sample
3 3 5 2 2
2 3 4
6 5 7
8 9 10
1 1 1 3 3
2 2 2 3 3 2
1 1 1 3 3
2 1 2 2 3 3
1 1 1 3 3
1
1
1
</p>