二维线段树原来就是线段树的节点上加个线段树- -
差分以(x, y)为中心向四周进行差分 这样就不用分别维护一阶差分和二阶差分什么的(如果以(1, 1) 为中心进行差分还要分开维护 而且涉及区间修改根本无法实现嘛)
还有hlq神犇提出了8二维树状数组的方法(太可怕了) 膜膜膜
这代码就6K啦? 有点不敢相信
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 500000;
inline LL gcd(LL a, LL b) {
while(b) {
LL r = a % b; a = b; b = r;
}
return a;
}
inline LL Abs(LL x) { return x < 0 ? -x : x; }
int n, m, q;
struct Array {
LL memory[MAXN+10];
LL * operator [] (const int x) { return &memory[(x-1)*n]; }
} a;
struct Seg {
Seg *ls, *rs;
LL val;
Seg () {
ls = rs = 0;
val = 0;
}
friend void build(Seg* &p, int L, int R, LL a[] ) {
p = new Seg;
if(L == R) {
p->val = a[L]; return ;
}
int mid = L+R >> 1;
build(p->ls, L, mid, a);
build(p->rs, mid+1, R, a);
p->val = gcd(p->ls->val, p->rs->val);
}
void ins(int L, int R, int x, LL v) {
if(L == R) {
this->val += v;
return ;
}
int mid = L+R >> 1;
if(x <= mid) ls->ins(L, mid, x, v);
else rs->ins(mid+1, R, x, v);
this->val = gcd(ls->val, rs->val);
}
LL query(int L, int R, int l, int r) {
if(l == L && R ==r) return val;
int mid = L+R >> 1;
if(r <= mid) return ls->query(L, mid, l, r);
if(l > mid) return rs->query(mid+1, R, l, r);
return gcd(ls->query(L, mid, l, mid), rs->query(mid+1, R, mid+1, r));
}
friend void Union(Seg* &p, Seg *l, Seg *r, int L, int R) {
if(!p) p = new Seg;
if(L == R) {
p->val = gcd(l->val, r->val);
return ;
}
int mid = L+R >> 1;
Union(p->ls, l->ls, r->ls, L, mid);
Union(p->rs, l->rs, r->rs, mid+1, R);
p->val = gcd(p->ls->val, p->rs->val);
}
} ;
struct Seg_Tree {
Seg_Tree *ls, *rs;
Seg *tree;
Seg_Tree () {
ls = rs = 0;
tree = 0;
}
friend void build(Seg_Tree* &p, int L, int R, Array &a) {
p = new Seg_Tree;
if(L == R) {
build(p->tree, 1, n, a[L]);
return ;
}
int mid = L+R >> 1;
build(p->ls, L, mid, a);
build(p->rs, mid+1, R, a);
Union(p->tree, p->ls->tree, p->rs->tree, 1, n);
}
void ins(int L, int R, int r, int c, LL val) {
if(L == R) {
tree->ins(1, n, c, val);
return ;
}
int mid = L+R >> 1;
if(r <= mid) ls->ins(L, mid, r, c, val);
else rs->ins(mid+1, R, r, c, val);
LL lson = ls->tree->query(1, n, c, c);
LL rson = rs->tree->query(1, n, c, c);
LL cur = tree->query(1, n, c, c);
tree->ins(1, n, c, gcd(lson, rson) - cur);
}
LL query(int L, int R, int l1, int r1, int l2, int r2) {
int mid = L+R >> 1;
if(L == l1 && r1 == R) return tree->query(1, n, l2, r2);
if(r1 <= mid) return ls->query(L, mid, l1, r1, l2, r2);
else if(l1 > mid) return rs->query(mid+1, R, l1, r1, l2, r2);
return gcd(ls->query(L, mid, l1, mid, l2, r2), rs->query(mid+1, R, mid+1, r1, l2, r2));
}
} *root;
int x, y;
int main() {
SF("%d%d%d%d%d", &m, &n, &x, &y, &q);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
SF("%lld", &a[i][j]);
for(int i = 1; i <= m; i++) {
for(int j = 1; j < y; j++) a[i][j] -= a[i][j+1];
for(int j = n; j > y; j--) a[i][j] -= a[i][j-1];
}
for(int j = 1; j <= n; j++) {
for(int i = 1; i < x; i++) a[i][j] -= a[i+1][j];
for(int i = m; i > x; i--) a[i][j] -= a[i-1][j];
}
build(root, 1, m, a);
for(int i = 1; i <= q; i++) {
int op, x1, y1, x2, y2;
LL c, p, val;
SF("%d%d%d%d%d", &op, &x1, &y1, &x2, &y2);
if(op == 0) {
x1 = x-x1; x2 = x+x2; y1 = y-y1; y2 = y+y2;
LL ans = root->query(1, m, x1, x2, y1, y2);
PF("%lld\n", Abs(ans));
}
else {
SF("%lld", &c);
if(x1 <= x && y1 <= y && x1-1 && y1-1)
root->ins(1, m, x1-1, y1-1, c);
else if(x1 <= x && y1 > y && x1-1)
root->ins(1, m, x1-1, y1, -c);
else if(x1 > x && y1 <= y && y1-1)
root->ins(1, m, x1, y1-1, -c);
else if(x1 > x && y1 > y)
root->ins(1, m, x1, y1, c);
if(x1 <= x && y2 >= y && x1-1 && y2+1 <= n)
root->ins(1, m, x1-1, y2+1, c);
else if(x1 <= x && y2 < y && x1-1)
root->ins(1, m, x1-1, y2, -c);
else if(x1 > x && y2 >= y && y2+1 <= n)
root->ins(1, m, x1, y2+1, -c);
else if(x1 > x && y2 < y)
root->ins(1, m, x1, y2, c);
if(x2 >= x && y1 <= y && x2+1 <= m && y1-1)
root->ins(1, m, x2+1, y1-1, c);
else if(x2 < x && y1 <= y && y1-1)
root->ins(1, m, x2, y1-1, -c);
else if(x2 >= x && y1 > y && x2+1 <= m)
root->ins(1, m, x2+1, y1, -c);
else if(x2 < x && y1 > y)
root->ins(1, m, x2, y1, c);
if(x2 >= x && y2 >= y && x2+1 <= m && y2+1 <= n)
root->ins(1, m, x2+1, y2+1, c);
else if(x2 < x && y2 >= y && y2+1 <= n)
root->ins(1, m, x2, y2+1, -c);
else if(x2 >= x && y2 < y && x2+1 <= m)
root->ins(1, m, x2+1, y2, -c);
else if(x2 < x && y2 < y)
root->ins(1, m, x2, y2, c);
if(x1 <= x && x2 >= x) {
if(y1 <= y && y1-1)
root->ins(1, m, x, y1-1, -c);
else if(y1 > y)
root->ins(1, m, x, y1, c);
if(y2 >= y && y2+1 <= n)
root->ins(1, m, x, y2+1, -c);
else if(y2 < y)
root->ins(1, m, x, y2, c);
}
if(y1 <= y && y2 >= y) {
if(x1 <= x && x1-1)
root->ins(1, m, x1-1, y, -c);
else if(x1 > x)
root->ins(1, m, x1, y, c);
if(x2 >= x && x2+1 <= m)
root->ins(1, m, x2+1, y, -c);
else if(x2 < x)
root->ins(1, m, x2, y, c);
}
if(x1 <= x && x2 >= x && y1 <= y && y2 >= y)
root->ins(1, m, x, y, c);
}
}
}