跳到正文

个人记录

11.14学习笔记&【二维线段树】详解

Lovesaint_lym2010约 26 分钟
原文
文章信息与版本

文章信息

发布
2024年11月14日
收录
2026年8月12日
更新
2026年11月13日
篇幅
约 26 分钟
获赞
1

版本

  1. v1当前

Part 1 引入

P4514 上帝造题的七分钟

有一个初始全为 00 的 n×mn \times m 的矩阵,有两种操作:

  • L a b c d data 将 (a,b)(a, b) 到 (c,d)(c, d) 这个矩阵的所有点的数都加上 datadata。
  • k a b c d 求 (a,b)(a, b) 到 (c,d)(c, d) 这个矩阵的所有点的数的总和。

我们数据结构名将线段树坐不住了,可是这**是二维的呀。于是线段树派出自己的弟弟二维线段树迎战。

二维线段树的名气比普通线段树小多了,而且应用范围也没有那么广(而且更难😡)。有两种写法可以写二维线段树:四叉树和树套树。两种写法的理论复杂度都是 O(nlog2n)O(nlog^2n) 的,只可惜写法较为简单的四叉树在随机数据下都无法证明复杂度可以到 O(nlog2n)O(nlog^2n),更何况特定数据下可能会到 O(n2)O(n^2),而且常数还挺大的,所以经常假到 O(n2)O(n^2) 以上。不过那也比暴力 n4n^4 好。

Part 2 四叉树

二维线段树同样可以采取普通线段树的思想。既然一个区间我们将其分割成左右两个区间,运用类比的思想,我们可以把一个矩形分割成左上,右上,左下,右下四个小矩形分别维护。

代码十分好理解,先放代码再详细说。(这段代码附了重新复制(change2))

C++
#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没有什么区别,只不过是把区间分成了四个部分。注意有的时候 x1=x2x_1 = x_2 或者 y1=y2y_1 = y_2 的时候二维区间退化成了一个一维区间,以上代码所写的 op=0op = 0 便是退化的这种情况。 还有就是 getSongetSon,让人非常难受,因为不像普通线段树一样 p×2,p×2+1p \times 2, p \times 2 + 1,而是有四种情况(四个儿子),getSongetSon 实现的就是找出他所有儿子的编号。

change

change1change1 与 change2change2 大同小异,以 change1change1 为例,如果此时这个区间与要更改的区间没有交集就 return,如果此时整个区间正好在要更改的区间之中,那就改值。否则就拆这个区间,与普通线段树没啥区别,还是注意一下退化的情况即可。具体见代码。

query

queryquery 就更简单了,与 changechange 基本相同,不多说了。

pushup

op=0op = 0 表示退化成一维的了,此时只有两个儿子 p×2,p×2+1p \times 2, p \times 2 + 1。op=1op = 1 的时候就代表正常情况,有 44 个儿子。

pushdown

tag1tag1 表示增加的 tagtag,tag2tag2 表示直接赋值的 tagtag。

tag2tag2 的优先级是大于 tag1tag1 的(change1change1 中在更改 tag1tag1 的时候 tag2tag2 也增加了,注意 tag2tag2 的初值为 −1-1。因为有可能被赋值为 00)。

好了这样的解法,P4514 这道题就拿到了 54pts54pts…

Part3 树套树

树套树有很多种例如线段树套线段树,线段树套平衡树,平衡树套线段树,树状数组套权值线段树,分块套树状数组等多种类型。我们要用的二维线段树,它所运用的是线段树套线段树。

顾名思义,线段树套线段树,就是线段树上每一个结点都是另一颗线段树。外层线段树记录 xx 轴,内层线段树记录 yy 轴。

例如外层线段树维护 [3,7][3, 7],那么维护的就是图中紫色部分:

                                          \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space

这个线段树套的线段树维护的是 [1,5][1, 5],那么维护的就是图中黄色部分:

                                     \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space

也就是外层维护的是 [l1,r1][l_1, r_1],内层维护的是 [l2,r2][l_2, r_2],那么总体维护的就是从 (l1,r1)∼(l2,r2)(l_1, r_1) \sim (l_2, r_2)。

标记永久化(小重点)

考虑一个问题,给定一个 n×mn \times m 的矩阵,初始所有值都是 00。

  • 第一种操作:将 (x1,y1)∼(x2,y2)(x_1, y_1) \sim (x_2, y_2) 全部赋值为 kk,其中 k≥k \geq 原来的值。
  • 第二种操作:求 (x1,y1)∼(x2,y2)(x_1, y_1) \sim (x_2, y_2) 最大值。

涉及到了矩阵修改,类比普通线段树的区间可以想到懒标记。但是如果直接加入懒标记会跑得很慢。归根结底,问题其实出现在懒惰标记的下传上。因为每个节点都对应一个线段树,每次 pushdown 的时候会对当前节点的儿子节点的整棵线段树进行。这就导致复杂度到了 O(nlogn)O(nlogn)。怎么办呢?我们考虑 标记永久化,就是修改时留下的懒标记不下传了,等每次访问这个节点的时候加上懒标记即可。

先给出普通线段树标记永久化的写法:

C++
#include <iostream>
using namespace std;
const int NR = 100010;
struct Node
{
    int l, r;
    long long d, tag;
} t[NR * 4];
int a[NR];
void build(int p, int l, int r)
{
    t[p].l = l, t[p].r = r;
    if (l == r)
    {
        t[p].d = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    t[p].d = t[p << 1].d + t[p << 1 | 1].d;
} 
void change(int p, int l, int r, int k)
{
	t[p].d += k * (r - l + 1);
    if (t[p].l == l && t[p].r == r)
    {
        t[p].tag += k;
        return;
    }
    int mid = (t[p].l + t[p].r) >> 1;
    if (l > mid) change(p << 1 | 1, l, r, k);
    else if (r <= mid) change(p << 1, l, r, k);
    else change(p << 1, l, mid, k), change(p << 1 | 1, mid + 1, r, k);
}
long long query(int p, int l, int r, long long sum)
{
    if (t[p].l == l && t[p].r == r) return t[p].d + sum * (r - l + 1);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l > mid) return query(p << 1 | 1, l, r, sum + t[p].tag);
    else if (r <= mid) return query(p << 1, l, r, sum + t[p].tag);
    else return query(p << 1, l, mid, sum + t[p].tag) + query(p << 1 | 1, mid + 1, r, sum + t[p].tag);
}
int main()
{
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    while (m--)
    {
        int op; cin >> op;
        if (op == 1)
        {
            int x, y; long long k;
            cin >> x >> y >> k;
            change(1, x, y, k);
        }
        else 
        {
            int x, y; cin >> x >> y;
            cout << query(1, x, y, 0) << '\n';
        }
    }
    return 0;
}

我们会发现 changechange 中的 pushdownpushdown 操作没了,pushuppushup 也没了。而 queryquery 函数多了一个参数 sumsum,这个就是记录懒标记的。之后每次加上访问到的节点的懒标记即可。

好了,真正树套树来了。下面给的是 SP1741 的代码,基本上可以算是板子了。

C++
#include <iostream>
#define ls p << 1
#define rs p << 1 | 1
using namespace std;
typedef long long ll;
struct NodeY
{
	int l, r;
	ll d, tag;
};
struct TreeY
{
	NodeY t[2510];
	void build(int p, int l, int r)
	{
		t[p].l = l, t[p].r = r, t[p].tag = -1;
		if (l == r) return;
		int mid = l + r >> 1;
		build(ls, l, mid), build(rs, mid + 1, r);
	}
	void change(int p, int l, int r, ll k)
	{
		t[p].d = max(t[p].d, k);
		if (t[p].l >= l && t[p].r <= r)
		{
			t[p].tag = max(t[p].tag, k);
			return;
		}
		int mid = t[p].l + t[p].r >> 1;
		if (r <= mid) change(ls, l, r, k);
		else if (l > mid) change(rs, l, r, k);
		else change(ls, l, mid, k), change(rs, mid + 1, r, k);
	}
	ll query(int p, int l, int r)
	{
		if (t[p].l >= l && t[p].r <= r) return t[p].d;
		int mid = t[p].l + t[p].r >> 1;
		ll tmp = t[p].tag;
		if (r <= mid) return max(tmp, query(ls, l, r));
		else if (l > mid) return max(query(rs, l, r), tmp);
		return max(tmp, max(query(ls, l, mid), query(rs, mid + 1, r)));
	}
};
struct NodeX
{
	int l, r;
	TreeY tag, tr;
};
struct TreeX
{
	int m;
	NodeX t[2510];
	void build(int p, int l, int r)
	{
		t[p].l = l, t[p].r = r;
		t[p].tr.build(1, 1, m);
		t[p].tag.build(1, 1, m);
		if (t[p].l == t[p].r) return;
		int mid = l + r >> 1;
		build(ls, l, mid), build(rs, mid + 1, r);
	}
	void change(int p, int l1, int r1, int l2, int r2, int k)
	{
		t[p].tr.change(1, l2, r2, k);
		if (t[p].l >= l1 && t[p].r <= r1)
		{
			t[p].tag.change(1, l2, r2, k);
			return;
		}
		int mid = t[p].l + t[p].r >> 1;
		if (r1 <= mid) change(ls, l1, r1, l2, r2, k);
		else if (l1 > mid) change(rs, l1, r1, l2, r2, k);
		else change(ls, l1, mid, l2, r2, k), change(rs, mid + 1, r1, l2, r2, k);
	}
	ll query(int p, int l1, int r1, int l2, int r2)
	{
		if (t[p].l >= l1 && t[p].r <= r1)
			return t[p].tr.query(1, l2, r2);
		int mid = (t[p].l + t[p].r) >> 1;
		ll tmp = t[p].tag.query(1, l2, r2);
		if (r1 <= mid) return max(tmp, query(ls, l1, r1, l2, r2));
		else if (l1 > mid) return max(tmp, query(rs, l1, r1, l2, r2));
		return max(tmp, max(query(ls, l1, mid, l2, r2), query(rs, mid + 1, r1, l2, r2)));
	}
} tree;
int n, m, Q;
int main()
{
	cin >> n >> m >> Q; n++; m++;
    tree.m = m; tree.build(1, 1, n);
    while(Q--)
	{
        int d, s, w, x, y;
        cin >> d >> s >> w >> x >> y;
        int tmp = tree.query(1, x + 1, x + d, y + 1, y + s);
        tree.change(1, x + 1, x + d, y + 1, y + s, tmp + w);
    }
    cout << tree.query(1, 1, n, 1, m) << '\n';
	return 0;
}

TreeYTreeY 表示内层线段树,TreeXTreeX 表示外层线段树。内层线段树与普通线段树没什么区别,主要说一下外层线段树。

  • 首先外层线段树的 tagtag 不再是简单的一个数了,而是一个线段树,因为不是所有的纵向区间的懒标记都一样。
  • 其次,changechange 的第一行,修改了内层线段树,这样不是相当于扩大了范围吗?

这是因为当前所覆盖的范围肯定包括了我们要修改的矩形,也就是说,当前结点的答案是有可能由修改的那个矩形中更新过来的。而如果我们仅仅在当前覆盖的范围等于我们要修改的矩形的范围是修改,那么在更大范围的查询时就会出问题

比如当前的横向区间是 [1,4][1,4],最大值为 11,而我们修改的矩形的左上角和右下角分别是 (2,2)(2,2) 和 (3,3)(3,3),修改后的值为 55。

如果没有这里的修改,再查询左上角和右下角分别是 (1,1)(1,1) 和 (4,4)(4,4) 的矩形的时候,我们得到的答案将会是 11。

可是修改貌似也不对。在一般情况下确实是错误的,这样的要求也造就了树套树写法的局限性。

对于矩阵推平 + 矩阵最大值的题目,我们只有在保证矩阵推平所赋的值不小于推平前的矩阵最大值的时候才保证树套树写法的正确性,求最小值同理。

至于为什么这种情况下是对的,显然,当前所覆盖的范围肯定包括了我们要修改的矩形,既然这个矩形被赋的值会大于当前区域的最大值,那么当前覆盖的范围的最大值一定来自那个矩形。

那为什么这道题是树套树呢?抽象题目可以得到:初始矩形为 00,每次将一个子矩形修改为这个子矩形的最大值 +h+h,求 qq 次修改之后整个矩形的最大值。板子。

缺陷

树套树就是局限性很大,只能做 我们只有在保证矩阵推平所赋的值不小于推平前的矩阵最大值的时候才保证树套树写法的正确性,求最小值同理。 同时,它也不可以求矩阵和,原因还是在与外层线段树的修改。这里会把修改的范围扩大。

尽管如此, 它的复杂度比四叉树好太多了,它真正的可以做到 O(nlog2n)O(nlog^2n)。

原文
存档评论1 条抓取于