#P2086. Magical Chessboard: GCD Queries and Updates

    ID: 15368 Type: Default 1000ms 256MiB

Magical Chessboard: GCD Queries and Updates

Magical Chessboard: GCD Queries and Updates

In this problem, you are given an N×MN \times M chessboard where each cell contains a positive integer. A board guardian is stationed at cell (X,Y)(X, Y) (1-indexed) and never moves. The guardian performs TT operations which are of two types:

  1. 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 a1,a2,,aka_1, a_2, \dots, a_k, you are to compute gcd(a1,a2,,ak)\gcd(a_1, a_2, \dots, a_k).

  2. Update Operation: The guardian selects an arbitrary rectangular subregion of the chessboard and adds a given integer vv to every number within that region.

    Your task is to process these TT 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 26212^{62} - 1.

inputFormat

The first line contains five integers NN, MM, TT, XX, and YY, where (X,Y)(X, Y) is the guardian's fixed position (1-indexed).

The next NN lines each contain MM positive integers representing the initial configuration of the chessboard.

Each of the following TT 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 (a,b)(a, b) and bottom-right corner (c,d)(c, d) (inclusive). It is guaranteed that aXca \le X \le c and bYdb \le Y \le d.

  • 2 a b c d v: An update operation where the integer vv is added to every cell in the rectangular region with top-left corner (a,b)(a, b) and bottom-right corner (c,d)(c, d) (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>