跳到正文

个人记录

树状数组与线段树的反思

Linshey约 16 分钟
原文
文章信息与版本

文章信息

发布
2020年8月13日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 16 分钟

版本

  1. v1当前

先开这两题: P4514上帝造题的七分钟 P1502 窗口的星星

我十分的开心:哇今天运气真好,秒出两道紫题,真是爽到了家。结果:调了一整天,一整天,一整天。。。

先讲讲第一道题:求实现二维的区间加和区间查询。我一看:二维树状数组水过去不就好了?于是WA了,样例都过不去。我就寻思我这树状数组打的有什么问题吗,猛然反应:我用二维树状数组维护差分的前缀和,其实我这么做一个queryquery其实等于那一格本身,而不是整个前驱矩形的和。

其实,我们要维护的,是二维差分数组的前缀和的前缀和!树状数组维护两重前缀和的一维模板如下:对于点ii,它对SSjSS_j的贡献等于ai∗(j−i+1)=ai∗(j+1)−ai∗ia_i* (j - i + 1)=a_i*(j+1)-a_i*i,所以我们维护ai∗ia_i*i和aia_i的前缀和,每次查询的时候再相减即可。

对于这题,我们为了简便思考,直接把差分数组当作原数组来思考,大大降低思维难度。一个点ai,ja_{i,j}对SSx,ySS_{x, y}的贡献等于ai,j∗(x−i+1)∗(y−j+1)a_{i,j}* (x-i+1)* (y-j+1)等于(x+1)(y+1)ai,j−(x+1)(j∗ai,j)−(y+1)(i∗ai,j)+ai,j∗i∗j(x+1)(y+1)a_{i,j}-(x+1)(j*a_{i,j})-(y+1)(i*a_{i,j})+a_{i,j}*i*j。于是我们分别维护ai,ja_{i,j}、ai,j∗ia_{i,j}*i、ai,j∗ja_{i,j}*j、ai,j∗i∗ja_{i,j}*i*j的前缀和,查询的时候就等于S4−(x+1)S3−(y+1)S2+(x+1)(y+1)S1S4-(x+1)S3-(y+1)S2+(x+1)(y+1)S1,再用常用的右上角减去那些那些的公式即可啦。

然后找虫子找了半天,究竟是哪里错了呢?原来是树状数组的板子错了,如下:

C++
int query(int k, int x, int y)
{
	int ret = 0;
	for (; x; x -= lowbit(x))
		for (; y; y -= lowbit(y))
			ret += tree[k - 1][x][y];
	return ret;
}

我喜欢不新建变量的写法,但是这么做的问题在于:(x,y)(x,y)不会被遍历到!于是改成下面这样:

C++
int ret = 0;
for (int i = x; i > 0; i -= lowbit(i))
	for (int j = y; j > 0; j -= lowbit(j))
		ret += tree[k - 1][i][j];
return ret;

这还没完,由于是前缀和,所以xx和yy可以等于00!所以我不假思索改成了下面这样:

C++
int ret = 0;
for (int i = x; i >= 0; i -= lowbit(i))
	for (int j = y; j >= 0; j -= lowbit(j))
		ret += tree[k - 1][i][j];
return ret;

然后死循环了,注意,零的lowbitlowbit不还是00!所以最后用整体位移,如下:

C++
x++; y++;
int ret = 0;
for (int i = x; i > 0; i -= lowbit(i))
	for (int j = y; j > 0; j -= lowbit(j))
		ret += tree[k - 1][i][j];
return ret;

完整代码如下:

C++
#include <iostream>

using namespace std;
const int maxn = 2049;

int n, m, tree[4][maxn][maxn];

inline int lowbit(int x)
{
	return x & -x;
}

void update(int x, int y, int k1, int k2, int k3, int k4)
{
	for (; x <= n; x += lowbit(x))
		for (; y <= m; y += lowbit(y))
		{
			tree[0][x][y] += k1;
			tree[1][x][y] += k2;
			tree[2][x][y] += k3;
			tree[3][x][y] += k4;
		}
}

int query(int k, int x, int y)
{
	int ret = 0;
	for (; x; x -= lowbit(x))
		for (; y; y -= lowbit(y))
			ret += tree[k - 1][x][y];
	return ret;
}

int query(int x, int y)
{
	int ret = 0;
	ret += query(4, x, y);
	ret += query(3, x, y) * (x + 1);
	ret += query(2, x, y) * (y + 1);
	ret += query(1, x, y) * (x + 1) * (y + 1);
	return ret;
}

int main()
{
	char op;
	cin >> op >> n >> m;
	while (cin >> op)
	{
		if (op == 'L')
		{
			int a, b, c, d, delta;
			cin >> a >> b >> c >> d >> delta;
			update(a, b, delta, delta * a, delta * b, delta * a * b);
			update(a, d + 1, -delta, -delta * a, -delta * (d + 1), -delta * a * (d + 1));
			update(c + 1, b, -delta, -delta * (c + 1), -delta * b, -delta * (c + 1) * b);
			update(c + 1, d + 1, delta, delta * (c + 1), delta * (d + 1), delta * (c + 1) * (d + 1));
		}
		else
		{
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			int ret = 0;
			ret += query(c, d);
			ret -= query(c, b - 1);
			ret -= query(a - 1, d);
			ret += query(a - 1, b - 1);
			cout << -ret << endl;
		}
	}
	return 0;
}

再来说说第二题:我们从左到右扫描,用尺取法保证左右宽度不超过WW(显然宽度越大越好嘛),接下来考虑纵向的。其实也就变成了求一个数据结构,能够快速返回序列中最大的长度为HH的子序列的总值。因此我们取前缀和SiS_i,用线段树的每个叶子节点ii维护Sumi−Sumi−H+1Sum_i-Sum_{i-H+1},非叶子节点维护下面的节点的最大值。容易发现每次修改一个点其实就是线段树上的区间加法,查询的返回值就是tree[root].valtree[root].val,于是这道紫题就秒掉了。但是:

C++

#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = 1e4;

int n, W, H;

struct node
{
	int x, y, l;
	bool operator <(const node b) const
	{
		return x < b.x;
	}
} pnt[maxn];

int tree[maxn], lson[maxn], rson[maxn], lazy[maxn], cnt;

int newnode()
{
	return ++cnt;
}

void pushdown(int x)
{
	if (lazy[x])
	{
		tree[lson[x]] += lazy[x];
		tree[rson[x]] += lazy[x];
		lazy[lson[x]] += lazy[x];
		lazy[lson[x]] += lazy[x];
		lazy[x] = 0;
	}
}

int pushup(int x)
{
	return max(tree[lson[x]], tree[rson[x]]);
}

void update(int x, int l, int r, int ll, int rr, int k)
{
	if (ll <= l && r <= rr)
	{
		tree[x] += k;
		lazy[x] += k;
		return;
	}
	pushdown(x);
	int mid = (l + r) / 2;
	if (mid >= ll)
	{
		if (!lson[x]) lson[x] = newnode();
		update(lson[x], l, mid, ll, rr, k);
	}
	if (rr > mid)
	{
		if (!rson[x]) rson[x] = newnode();
		update(rson[x], mid + 1, r, ll, rr, k);
	}
	tree[x] = pushup(x);
}

int main()
{
	int T; cin >> T;
	while (T--) {
		cin >> n >> W >> H;
		for (int i = 1; i <= n; i++)
		{
			cin >> pnt[i].x >> pnt[i].y >> pnt[i].l;
		}
		sort(pnt + 1, pnt + n + 1);
		int l = 1, root = newnode(), ans = 0;
		for (int i = 1; i <= n; i++)
		{
			update(root, -1e9, 1e9, pnt[i].y, pnt[i].y + H - 1, pnt[i].l);
			while (pnt[i].x == pnt[i + 1].x)
			{
				++i;
				update(root, -1e9, 1e9, pnt[i].y, pnt[i].y + H - 1, pnt[i].l);
			}
			while (pnt[i].x - pnt[l].x + 1 > W)
			{
				update(root, -1e9, 1e9, pnt[l].y, pnt[l].y + H - 1, -pnt[l].l);
				l++;
			}
			ans = max(ans, tree[root]);
		}
		cout << ans << endl;
		while (l <= n)
		{
			update(root, -1e9, 1e9, pnt[l].y, pnt[l].y + H - 1, -pnt[l].l);
			l++;
		}
	}
	return 0;
}

10分。怎么错的呢?首先递归边界−1e9-1e9~1e91e9是错的,负数不能成为线段树的定义域,否则死循环警告。应该改一改,开long longlong\ long。假如只是这一个bugbug我也不会来写文章的。还有的BUG是:我把很多的操作无害\color{red}\text{无害}地往节点00去丢,以此免去动态开点线段树很多判断有没有这个儿子地麻烦,但是你看下面这个函数:

C++
int pushup(int x)
{
	return max(tree[lson[x]], tree[rson[x]]);
}

是不是发现了什么?所以改成这样:

C++
int pushup(int x)
{
    //return max(tree[lson[x]], tree[rson[x]]);
    int ret = 0;
    if (lson[x]) ret = max(ret, tree[lson[x]]);
    if (rson[x]) ret = max(ret, tree[rson[x]]);
    return ret;
}

这还没完呢,看下面这个函数:

C++
void pushdown(int x)
{
	if (lazy[x])
	{
		tree[lson[x]] += lazy[x];
		tree[rson[x]] += lazy[x];
		lazy[lson[x]] += lazy[x];
		lazy[rson[x]] += lazy[x];
		lazy[x] = 0;
	}
}

看起来人畜无害,没有这个儿子就往节点00放,对吧?可是,万一将来这个点被建出来了呢?岂不是少了一些从lazylazy里面来的值?因此改成下面这样:

C++
void pushdown(int x)
{
	if (lazy[x])
	{
		if (!lson[x]) lson[x] = newnode();
		if (!rson[x]) rson[x] = newnode();
		tree[lson[x]] += lazy[x];
		tree[rson[x]] += lazy[x];
		lazy[lson[x]] += lazy[x];
		lazy[rson[x]] += lazy[x];
		lazy[x] = 0;
	}
}

这给我们将来写动态开点线段树的启示是:只要这个点有被调用到,那么就一定把这个点建出来,不要往节点00上丢!

代码:

C++
#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long

using namespace std;
const int maxn = 1e4 +1e2;
const int INF = (1ull << 31) + 1e7;

int n, W, H;

struct node
{
	int x, y, l;
	bool operator <(const node b) const
	{
		return x < b.x;
	}
} pnt[maxn];

int tree[maxn * 40], lson[maxn * 40], rson[maxn * 40], lazy[maxn * 40], cnt;

int newnode()
{
	return ++cnt;
}

void pushdown(int x)
{
	if (lazy[x])
	{
		if (!lson[x]) lson[x] = newnode();
		if (!rson[x]) rson[x] = newnode();
		tree[lson[x]] += lazy[x];
		tree[rson[x]] += lazy[x];
		lazy[lson[x]] += lazy[x];
		lazy[rson[x]] += lazy[x];
		lazy[x] = 0;
	}
}

int pushup(int x)
{
	//return max(tree[lson[x]], tree[rson[x]]);
	int ret = 0;
	if (lson[x]) ret = max(ret, tree[lson[x]]);
	if (rson[x]) ret = max(ret, tree[rson[x]]);
	return ret;
}

void update(int x, int l, int r, int ll, int rr, int k)
{
	if (ll <= l && r <= rr)
	{
		tree[x] += k;
		lazy[x] += k;
		return;
	}
	pushdown(x);
	int mid = (l + r) / 2;
	if (mid >= ll)
	{
		if (!lson[x]) lson[x] = newnode();
		update(lson[x], l, mid, ll, rr, k);
	}
	if (rr > mid)
	{
		if (!rson[x]) rson[x] = newnode();
		update(rson[x], mid + 1, r, ll, rr, k);
	}
	tree[x] = pushup(x);
}

signed main()
{
	int T; cin >> T;
	while (T--) {
		memset(tree, 0, sizeof(tree));
		memset(lson, 0, sizeof(lson));
		memset(rson, 0, sizeof(rson));
		memset(lazy, 0, sizeof(lazy));
		cnt = 0;
		cin >> n >> W >> H;
		for (int i = 1; i <= n; i++)
		{
			cin >> pnt[i].x >> pnt[i].y >> pnt[i].l;
		}
		sort(pnt + 1, pnt + n + 1);
		int l = 1, root = newnode(), ans = 0;
		for (int i = 1; i <= n; i++)
		{
			update(root, 0, INF, pnt[i].y, pnt[i].y + H - 1, pnt[i].l);
			while (pnt[i].x == pnt[i + 1].x && i < n)
			{
				++i;
				update(root, 0, INF, pnt[i].y, pnt[i].y + H - 1, pnt[i].l);
			}
			while (pnt[i].x - pnt[l].x + 1 > W)
			{
				update(root, 0, INF, pnt[l].y, pnt[l].y + H - 1, -pnt[l].l);
				l++;
			}
			ans = max(ans, tree[root]);
		}
		cout << ans << endl;
		while (l <= n)
		{
			update(root, 0, INF, pnt[l].y, pnt[l].y + H - 1, -pnt[l].l);
			l++;
		}
	}
	return 0;
}
原文
存档评论4 条抓取于