1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
| #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;
#define ll long long
namespace fast_io{
//...
}using namespace fast_io;
const int MAXN = 510000;
ll gcd(ll a,ll b){
return b==0?abs(a):gcd(b,a%b);
}
#define lson (nown<<1)
#define rson (nown<<1|1)
#define mid ((l+r)>>1)
struct SegTree1D{
ll *gg;
SegTree1D(int m){gg = new ll[m<<2];}
void build(int nown,int l,int r,const ll *lt,const ll *rt){
if(r < l) return gg[nown] = 0,void();
if(l == r) gg[nown] = rt == NULL?lt[l]: gcd(lt[nown],rt[nown]);
else{
build(lson,l,mid,lt,rt),build(rson,mid+1,r,lt,rt);
gg[nown] = gcd(gg[lson],gg[rson]);
}
}
ll query(int nown,int l,int r,int ql,int qr){
if(qr < l || r < ql) return 0;
if(ql <= l && r <= qr) return gg[nown];
else{
ll ans = 0;
if(ql <= mid) ans = gcd(ans,query(lson,l,mid,ql,qr));
//if(ans == 1) return ans;
if(qr >= mid+1) ans = gcd(ans,query(rson,mid+1,r,ql,qr));
return ans;
}
}
void modify(int nown,int l,int r,int pos,ll v){
if(pos < l || pos > r) return;
if(l == r) gg[nown] += v;
else{
if(pos <= mid) modify(lson,l,mid,pos,v);
else modify(rson,mid+1,r,pos,v);
gg[nown] = gcd(gg[lson],gg[rson]);
}
}
void modify(int nown,int l,int r,int pos,const ll *ltree,const ll *rtree){
if(pos < l || pos > r) return;
if(l == r) gg[nown] = gcd(ltree[nown],rtree[nown]);
else{
if(pos <= mid) modify(lson,l,mid,pos,ltree,rtree);
else modify(rson,mid+1,r,pos,ltree,rtree);
gg[nown] = gcd(ltree[nown],rtree[nown]);
}
}
}*C,*D;
int n,m,xx,yy,t;
ll *a[MAXN],*b[MAXN];
ll c[MAXN],d[MAXN],val;//c横d纵
struct SegTree2D{
// 每一行建1棵1D线段树
// 一共n棵,每棵大小均为m
SegTree1D *t[MAXN<<2];
int a,b,c,d;//行从 a->b, 列从 c->d or (a,b)
ll v;
void build(int nown,int l,int r,int m,ll **num){
t[nown] = new SegTree1D(m);
if(l == r) t[nown]->build(1,1,m,num[l],NULL);
else{
build(lson,l,mid,m,num),build(rson,mid+1,r,m,num);
t[nown]->build(1,1,m,t[lson]->gg,t[rson]->gg);
}
}
void modify(int nown,int l,int r){//(a,b) += v
if(r < a || l > a) return;
if(l == r) t[nown]->modify(1,1,m,b,v);
else{
if(a <= mid) modify(lson,l,mid);
else modify(rson,mid+1,r);
t[nown]->modify(1,1,m,b,t[lson]->gg,t[rson]->gg);
}
}
ll query(int nown,int l,int r){
if(b < l || r < a) return 0;
if(a <= l && r <= b) return t[nown]->query(1,1,m,c,d);
else{
ll ans = 0;
if(a <= mid) ans = gcd(ans,query(lson,l,mid));
//if(ans == 1) return ans;
if(b >= mid+1) ans = gcd(ans,query(rson,mid+1,r));
return ans;
}
}
}T;
void modify(int x,int y,ll v){
T.a = x,T.b = y,T.v = v;
if(1 <= x && x <= n && 1 <= y && y <= m) T.modify(1,1,n);
}
ll query(int x1,int x2,int y1,int y2){
ll ans = val;
x1 = xx - x1,x2 = xx + x2,y1 = yy - y1,y2 = yy + y2;
ans = gcd(ans,gcd(D->query(1,1,n,x1+1,x2),C->query(1,1,m,y1+1,y2)));
T.a = x1+1,T.b = x2,T.c = y1+1,T.d = y2;
ans = gcd(ans,T.query(1,1,n));
return abs(ans);
}
void modify(int x1,int x2,int y1,int y2,ll v){
if(x1 <= xx && xx <= x2 && y1 <= yy && yy <= y2) val += v;
if(x1 <= xx && xx <= x2) C->modify(1,1,m,y1,v),C->modify(1,1,m,y2+1,-v);
if(y1 <= yy && yy <= y2) D->modify(1,1,n,x1,v),D->modify(1,1,n,x2+1,-v);
modify(x1,y1,v),modify(x1,y2+1,-v),modify(x2+1,y1,-v),modify(x2+1,y2+1,v);
}
void init(){
read(n),read(m),read(xx),read(yy),read(t);
for(int i = 0;i<=n+10;i++){
ll t = m+10;
a[i] = new ll[t],b[i] = new ll[t];
memset(a[i],0,sizeof(ll) * t),memset(b[i],0,sizeof(ll) * t);
}
for(int i = 1;i<=n;i++)
for(int j = 1;j<=m;j++)
read(a[i][j]);
}
void build(){
for(int i = 1;i<=n;i++)
for(int j = 1;j<=m;j++)
b[i][j] = a[i][j] - a[i-1][j] - a[i][j-1] + a[i-1][j-1];
for(int i = 1;i<=m;i++) c[i] = a[xx][i] - a[xx][i-1];
for(int i = 1;i<=n;i++) d[i] = a[i][yy] - a[i-1][yy];
val = a[xx][yy];
C = new SegTree1D(m),D = new SegTree1D(n);
C->build(1,1,m,c,NULL),D->build(1,1,n,d,NULL);
T.build(1,1,n,m,b);
}
void solve(){
static int op,x1,x2,y1,y2;
static ll v;
for(int i = 1;i<=t;i++){
read(op),read(x1),read(y1),read(x2),read(y2);
if(op == 0){
print(query(x1,x2,y1,y2)),print('\n');
}
else{
read(v);
modify(x1,x2,y1,y2,v);
}
}
}
signed main(){
init();
build();
solve();
flush();
return 0;
}
|