#include
using namespace std;
typedef long long dint; #ifdef WIN32 #define lld “%I64d” #else #define lld “%lld” #endif #define _l (long long int)
#define checkNeg(x) {
if (x < 0)
x = -x;
}
dint gcd(dint a, dint b) {
checkNeg(a);
checkNeg(b);
while (a) {
register dint c(b % a);
b = a;
a = c;
}
return b;
}
#define md ((p->l+p->r)»1) #define mdp(p) ((p->l+p->r)»1) #define zp(x,y) ((x)*m+(y))
const int maxn = 500003;
int n, m, q, xo, yo; dint a[maxn], b[maxn];
namespace isgt { const int maxl = 27; const int maxnd = maxn * 27; struct seg { int l, r; dint g; seg ls, rs; }; seg sbuf_arr[maxnd], sbuf(sbuf_arr); inline seg build(int l, int r, dint a) { seg p(sbuf ++); p-> l = l, p-> r = r; if (l + 1 == r) { p-> g = a[l]; } else { p-> ls = build(l, md, a); p-> rs = build(md, r, a); p-> g = gcd(p-> ls-> g, p-> rs-> g); } return p; } void chg(seg* p, int po, dint v, int ty) { seg* s[maxl]; int t(1); for (s[t] = p; s[t]-> l + 1 < s[t]-> r; ++ t) if (po < mdp(s[t])) s[t + 1] = s[t]-> ls; else s[t + 1] = s[t]-> rs; if (ty == 0) s[t]-> g += v; else s[t]-> g = v; while (– t) s[t]-> g = gcd(s[t]-> ls-> g, s[t]-> rs-> g); } inline dint qry(seg* p, int l, int r) { if (p-> l == l && p-> r == r) return p-> g; else if (r <= md) return qry(p-> ls, l, r); else if (l >= md) return qry(p-> rs, l, r); else return gcd(qry(p-> ls, l, md), qry(p-> rs, md, r)); } };
namespace osgt { struct seg { int l, r; isgt :: seg* rt; seg *ls, *rs; }; seg sbuf_arr[maxn * 3], *sbuf(sbuf_arr); dint tmp[maxn];
inline void update(seg* p, int po) {
dint va(isgt :: qry(p-> ls-> rt, po, po + 1));
dint vb(isgt :: qry(p-> rs-> rt, po, po + 1));
isgt :: chg(p-> rt, po, gcd(va, vb), 1);
}
seg* build(int l, int r, int rl, int rr) {
seg* p(sbuf ++);
p-> l = l, p-> r = r;
if (l + 1 == r) {
p-> rt = isgt :: build(rl, rr, b + l * m);
}
else {
p-> ls = build(l, md, rl, rr);
p-> rs = build(md, r, rl, rr);
p-> rt = isgt :: build(rl, rr, b + l * m);
for (int i = rl; i < rr; ++ i)
update(p, i);
}
return p;
}
inline dint qry(seg* p, int l, int r, int rl, int rr) {
if (p-> l == l && p-> r == r)
return isgt :: qry(p-> rt, rl, rr);
else if (r <= md)
return qry(p-> ls, l, r, rl, rr);
else if (l >= md)
return qry(p-> rs, l, r, rl, rr);
else
return gcd(qry(p-> ls, l, md, rl, rr), qry(p-> rs, md, r, rl, rr));
}
inline void chg(seg* p, int px, int py, dint vo) {
if (p-> l + 1 == p-> r)
isgt :: chg(p-> rt, py, vo, 0);
else {
if (px < md)
chg(p-> ls, px, py, vo);
else
chg(p-> rs, px, py, vo);
update(p, py);
}
}
};
osgt :: seg *rt[4];
void preArr() { for (int i = 0; i < n; ++ i) for (int j = 0; j < m; ++ j) scanf(lld, a + zp(i, j)); for (int i = 0; i < xo; ++ i) { for (int j = 0; j < yo; ++ j) b[zp(i, j)] = a[zp(i, j)] - a[zp(i + 1, j)] - a[zp(i, j + 1)] + a[zp(i + 1, j + 1)]; b[zp(i, yo)] = a[zp(i, yo)] - a[zp(i + 1, yo)]; for (int j = yo + 1; j < m; ++ j) b[zp(i, j)] = a[zp(i, j)] - a[zp(i + 1, j)] - a[zp(i, j - 1)] + a[zp(i + 1, j - 1)]; } for (int j = 0; j < yo; ++ j) b[zp(xo, j)] = a[zp(xo, j)] - a[zp(xo, j + 1)]; b[zp(xo, yo)] = a[zp(xo, yo)]; for (int j = yo + 1; j < m; ++ j) b[zp(xo, j)] = a[zp(xo, j)] - a[zp(xo, j - 1)]; for (int i = xo + 1; i < n; ++ i) { for (int j = 0; j < yo; ++ j) b[zp(i, j)] = a[zp(i, j)] - a[zp(i - 1, j)] - a[zp(i, j + 1)] + a[zp(i - 1, j + 1)]; b[zp(i, yo)] = a[zp(i, yo)] - a[zp(i - 1, yo)]; for (int j = yo + 1; j < m; ++ j) b[zp(i, j)] = a[zp(i, j)] - a[zp(i - 1, j)] - a[zp(i, j - 1)] + a[zp(i - 1, j - 1)]; } rt[0] = osgt :: build(0, xo + 1, 0, yo + 1); rt[1] = osgt :: build(0, xo + 1, yo, m); rt[2] = osgt :: build(xo, n, 0, yo + 1); rt[3] = osgt :: build(xo, n, yo, m); }
int main() { #ifdef LAEKOV_LOCAL freopen(".in", “r”, stdin); #endif
scanf("%d%d%d%d%d", &n, &m, &xo, &yo, &q);
-- xo, -- yo;
preArr();
while (q --) {
int opt, xa, ya, xb, yb;
scanf("%d%d%d%d%d", &opt, &xa, &ya, &xb, &yb);
if (opt == 0) {
dint v0(osgt :: qry(rt[0], xo - xa, xo + 1, yo - ya, yo + 1));
dint v1(osgt :: qry(rt[1], xo - xa, xo + 1, yo, yo + yb + 1));
dint v2(osgt :: qry(rt[2], xo, xo + xb + 1, yo - ya, yo + 1));
dint v3(osgt :: qry(rt[3], xo, xo + xb + 1, yo, yo + yb + 1));
//printf(lld "\n", v3);
printf(lld "\n", gcd(gcd(v0, v1), gcd(v2, v3)));
}
else {
dint v;
scanf(lld, &v);
-- xa, -- ya, -- xb, -- yb;
if (xa <= xo && ya <= yo) {
int xc(min(xo, xb)), yc(min(yo, yb));
osgt :: chg(rt[0], xc, yc, v);
if (xa)
osgt :: chg(rt[0], xa - 1, yc, -v);
if (ya)
osgt :: chg(rt[0], xc, ya - 1, -v);
if (xa && ya)
osgt :: chg(rt[0], xa - 1, ya - 1, v);
}
if (xa <= xo && yb >= yo) {
int xc(min(xo, xb)), yc(max(yo, ya));
osgt :: chg(rt[1], xc, yc, v);
if (xa)
osgt :: chg(rt[1], xa - 1, yc, -v);
if (yb + 1 < m)
osgt :: chg(rt[1], xc, yb + 1, -v);
if (xa && yb + 1 < m)
osgt :: chg(rt[1], xa - 1, yb + 1, v);
}
if (xb >= xo && ya <= yo) {
int xc(max(xo, xa)), yc(min(yo, yb));
osgt :: chg(rt[2], xc, yc, v);
if (xb + 1 < n)
osgt :: chg(rt[2], xb + 1, yc, -v);
if (ya)
osgt :: chg(rt[2], xc, ya - 1, -v);
if (xb + 1 < n && ya)
osgt :: chg(rt[2], xb + 1, ya - 1, v);
}
if (xb >= xo && yb >= yo) {
int xc(max(xo, xa)), yc(max(yo, ya));
osgt :: chg(rt[3], xc, yc, v);
if (xb + 1 < n)
osgt :: chg(rt[3], xb + 1, yc, -v);
if (yb + 1 < m)
osgt :: chg(rt[3], xc, yb + 1, -v);
if (xb + 1 < n && yb + 1 < m)
osgt :: chg(rt[3], xb + 1, yb + 1, v);
}
}
}
}