Magic Chessboard

고정된 위치를 기준으로 하는 직사각형 영역의 최대공약수를 구하는 질의와, 임의의 직사각형 영역에 같은 값을 더하는 갱신을 함께 처리한다. N 곱하기 M은 500000 이하이고 연산 수는 100000 이하이다. 문제에서 주어진 조건만으로 판단할 때 2차원 GCD 세그먼트 트리와 차분 배열을 결합해야 하는 매우 어려운 문제이다. 인터뷰 문제가 아니라 대회용 고난도 문제에 해당한다. 19930324 같은 특수한 숫자는 정답 횟수와 관련된 장치일 뿐 알고리즘에는 영향을 주지 않는다. 갱신이 값을 더하는 형태이므로 GCD의 차분 성질을 이용해야 한다. 각 행과 열에 대해 차분 배열을 관리하고 GCD 세그먼트 트리로 구간 GCD를 유지하는 방식이 필요하다. 쿼리 영역이 고정된 위치를 기준으로 확장되므로 그 점을 활용한 최적화가 가능하다. 난이도는 9로 평가한다.

어려움9세그먼트 트리정수론수학행렬아직 제출이 없습니다시간 제한5초메모리 제한512 MB

문제

Just going into the second grade, little Q has purchased a popular educational game — the magic chessboard! This game takes place on an N row by M column grid, where each grid cell contains a single positive integer. The chessboard guardian is located at row X, column Y(rows and columns are numbered starting from 1) and will never move. The chessboard guardian will perform two types of operations:

  1. Query: Using his current location as a base, he will expand outwards in 4 directions to produce a rectangle of variable size. Then, you will be asked for the greatest common divisor of all integers in the region.
  2. Update: He will randomly choose a rectangular region on the chessboard and simultaneously add a fixed value to the integers on each of the cells in the region.

The game's instruction manual contains an message reading "My smart young friends, when you have continuously answered 19930324 correct queries, there will be a surprise!" Little Q is incredibly eager to discover the surprise, so he plays this game every day. However due to his carelessness, mistakes are very often made. So, he has come to you for help, hoping that you can write a program to help him correctly answer the chessboard guardian's queries 100% of the time.

To make this problem simpler, your program will only need to complete T operations of the chessboard guardian. It is guaranteed that all numbers on the chessboard will be positive integers not exceeding 262 − 1.

입력

The first line of input contains two positive integers N and M, representing the dimensions of the chessboard.

The second line contains two positive integers X and Y, representing the chessboard guardian's position.

The third line contains a positive integer T, representing the number of operations carried out by the chessboard guardian.

For the following N lines, each line contains M integers, describing the numbers in all of the grid cells.

For the following T lines, each line will describe a single operation. Each line will start with either the number 0 or 1:

  • If the line starts with a 0, then this operation is a query. Four nonnegative integers x1, y1, x2, and y2 will follow. This indicates that the query range of the chessboard guardian will span x1 rows above him, x2 rows below him, y1 columns to his left, and y2 columns to his right (refer to the sample below).
  • If the line starts with a 1, then this operation is an update. Four positive integers x1, y1, x2, y2 and an integer c will follow. This indicates that the upper and lower boundaries of the updated region are respectively x1 and x2, while the left and right boundaries of the region are respectively y1 and y2 (refer to the sample below). All of the numbers in this range are simultaneously incremented by c (note that c can be negative).

출력

For each query, output a single number on a separate line representing the greatest common divisor of the queried region.

제한

  • N × M ≤ 500000
  • T ≤ 100000.