11.14学习笔记&【二维线段树】详解
Part 1 引入
P4514 上帝造题的七分钟
有一个初始全为
0 的n \times m 的矩阵,有两种操作:
L a b c d data将(a, b) 到(c, d) 这个矩阵的所有点的数都加上data 。k a b c d求(a, b) 到(c, d) 这个矩阵的所有点的数的总和。
我们数据结构名将线段树坐不住了,可是这**是二维的呀。于是线段树派出自己的弟弟二维线段树迎战。
二维线段树的名气比普通线段树小多了,而且应用范围也没有那么广(而且更难😡)。有两种写法可以写二维线段树:四叉树和树套树。两种写法的理论复杂度都是 不过那也比暴力 。
Part 2 四叉树
二维线段树同样可以采取普通线段树的思想。既然一个区间我们将其分割成左右两个区间,运用类比的思想,我们可以把一个矩形分割成左上,右上,左下,右下四个小矩形分别维护。
代码十分好理解,先放代码再详细说。(这段代码附了重新复制(change2))
#include <iostream>
using namespace std;
typedef long long ll;
const int NR = 1010;
struct Node
{
int x1, y1, x2, y2;
ll tag1, tag2, d;
} t[NR * NR << 2];
#define x1(p) t[p].x1
#define y1(p) t[p].y1
#define x2(p) t[p].x2
#define y2(p) t[p].y2
#define tag1(p) t[p].tag1
#define tag2(p) t[p].tag2
#define d(p) t[p].d
int getSon(int p, int x)
{ return p * 4 - 2 + x; }
int n, m, q;
ll a[NR][NR];
void pushup(int p, int op)
{
d(p) = d(getSon(p, 0)) + d(getSon(p, 1));
if (op == 0) return;
for (int i = 2; i < 4; i++) d(p) += d(getSon(p, i));
}
void pushdown(int p)
{
if (t[p].tag1 != 0)
{
if (t[p].y1 == t[p].y2)
{
int s0 = getSon(p, 0), s1 = getSon(p, 1);
tag1(s0) += tag1(p);
tag1(s1) += tag1(p);
d(s0) += (x2(s0) - x1(s0) + 1) * tag1(p);
d(s1) += (x2(s1) - x1(s1) + 1) * tag1(p);
}
else if(t[p].x1 == t[p].x2)
{
int s0 = getSon(p, 0), s1 = getSon(p, 1);
tag1(s0) += tag1(p);
tag1(s1) += tag1(p);
d(s0) += (y2(s0) - y1(s0) + 1) * tag1(p);
d(s1) += (y2(s1) - y1(s1) + 1) * tag1(p);
}
else
{
for (int i = 0; i < 4; i++)
{
int son = getSon(p, i), s = (x2(son) - x1(son) + 1) * ((y2(son) - y1(son) + 1));
d(son) += s * tag1(p);
tag1(son) += tag1(p);
}
}
tag1(p) = 0;
}
if (tag2(p) >= 0)
{
if (x1(p) == x2(p))
{
int s0 = getSon(p, 0), s1 = getSon(p, 1);
tag2(s0) = tag2(p);
tag2(s1) = tag2(p);
tag1(s0) = tag1(s1) = 0;
d(s0) = (y2(s0) - y1(s0) + 1) * tag2(p);
d(s1) = (y2(s1) - y1(s1) + 1) * tag2(p);
}
else if (y1(p) == y2(p))
{
int s0 = getSon(p, 0), s1 = getSon(p, 1);
tag2(s0) = tag2(p);
tag2(s1) = tag2(p);
tag1(s0) = tag1(s1) = 0;
d(s0) = (x2(s0) - x1(s0) + 1) * tag2(p);
d(s1) = (x2(s1) - x1(s1) + 1) * tag2(p);
}
else
{
for (int i = 0; i < 4; i++)
{
int son = getSon(p, i), s = (x2(son) - x1(son) + 1) * ((y2(son) - y1(son) + 1));
tag1(son) = 0;
d(son) = s * tag2(p);
tag2(son) = tag2(p);
}
}
tag2(p) = -1;
}
}
void build (int p, int x1, int y1, int x2, int y2)
{
x1(p) = x1, y1(p) = y1, x2(p) = x2, y2(p) = y2;
tag1(p) = 0, tag2(p) = -1;
if (x1 == x2 && y1 == y2)
{
d(p) = a[x1][y1];
return;
}
int midx = x1 + x2 >> 1, midy = y1 + y2 >> 1;
if (x1 == x2)
{
build(getSon(p, 0), x1, y1, x2, midy);
build(getSon(p, 1), x1, midy + 1, x2, y2);
pushup(p, 0);
}
else if (y1 == y2)
{
build(getSon(p, 0), x1, y1, midx, y2);
build(getSon(p, 1), midx + 1, y1, x2, y2);
pushup(p, 0);
}
else
{
build(getSon(p, 0), x1, y1, midx, midy);
build(getSon(p, 1), midx + 1, y1, x2, midy);
build(getSon(p, 2), x1, midy + 1, midx, y2);
build(getSon(p, 3), midx + 1, midy + 1, x2, y2);
pushup(p, 1);
}
}
void change1(int p, int x1, int y1, int x2, int y2, int val)
{
if (x1(p) > x2 || x2(p) < x1 || y1(p) > y2 || y2(p) < y1) return;
if (x1(p) >= x1 && x2(p) <= x2 && y1(p) >= y1 && y2(p) <= y2)
{
tag1(p) += val;
if (tag2(p) >= 0) tag2(p) += val;
d(p) += (y2(p) - y1(p) + 1) * (x2(p) - x1(p) + 1) * val;
return;
}
pushdown(p);
if (x1(p) == x2(p) || y1(p) == y2(p))
{
change1(getSon(p, 0), x1, y1, x2, y2, val);
change1(getSon(p, 1), x1, y1, x2, y2, val);
pushup(p, 0);
}
else
{
for (int i = 0; i < 4; i++) change1(getSon(p, i), x1, y1, x2, y2, val);
pushup(p, 1);
}
}
void change2(int p, int x1, int y1, int x2, int y2, int val)
{
if (x1(p) > x2 || x2(p) < x1 || y1(p) > y2 || y2(p) < y1) return;
if (x1(p) >= x1 && x2(p) <= x2 && y1(p) >= y1 && y2(p) <= y2)
{
tag1(p) = 0, tag2(p) = val;
d(p) = (y2(p) - y1(p) + 1) * (x2(p) - x1(p) + 1) * val;
return;
}
pushdown(p);
if (x1(p) == x2(p) || y1(p) == y2(p))
{
change1(getSon(p, 0), x1, y1, x2, y2, val);
change1(getSon(p, 1), x1, y1, x2, y2, val);
pushup(p, 0);
}
else
{
for (int i = 0; i < 4; i++) change1(getSon(p, i), x1, y1, x2, y2, val);
pushup(p, 1);
}
}
ll query (int p, int x1, int y1, int x2, int y2)
{
if (x1(p) > x2 || x2(p) < x1 || y1(p) > y2 || y2(p) < y1) return 0;
if (x1(p) >= x1 && x2(p) <= x2 && y1(p) >= y1 && y2(p) <= y2) return d(p);
pushdown(p);
if (x1(p) == x2(p) || y1(p) == y2(p))
return query(getSon(p, 0), x1, y1, x2, y2) + query(getSon(p, 1), x1, y1, x2, y2);
else
{
ll res = 0;
for (int i = 0; i < 4; i++)
res += query(getSon(p, i), x1, y1, x2, y2);
return res;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
char c;
cin >> c >> n >> m;
// for (int i = 1; i <= n; i++)
// for (int j = 1; j <= m; j++)
// cin >> a[i][j];
build(1, 1, 1, n, m);
while (cin >> c)
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (c == 'L')
{
ll val; cin >> val;
change1(1, x1, y1, x2, y2, val);
}
else cout << query(1, x1, y1, x2, y2) << '\n';
}
return 0;
}
build
与普通线段树的build没有什么区别,只不过是把区间分成了四个部分。注意有的时候