跳到正文

算法·理论

线段树的 lazy tag

ni_ju_ge约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月13日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

众所周知,线段数是一种数据类型,当它没有 lazy tag 时,就是个 fw,查找和修改都比数组还慢,但!是!当它得到了 神器-lazy tag,区间查找与修改的效率都将成倍增加,那么,lazy tag 到底是怎么实现的呢?

lazy tag

lazy tag 的作用

普通的线段树,在查找时需要找到具体的叶子节点,才能修改叶子节点的值,但当一个节点表示的区间正好被包括在需修改区间中时,就可以通过只修改该节点的值来快速完成操作。

在上述情况下,可以先修改该节点的值,再将该节点打上 lazy tag,表示该结点以下的节点未修改。当需要调用时,就将需修改节点进行修改,然后再把 lazy tag 下传,就可以实现区间的修改和调用操作了。

附上一个演示视频。侵权删。

lazy tag 的实现方式

存储

可以使用 struct 建立一个结构体,做为线段树的节点,其中应当包含左端点、右端点、数据和 lazy tag。

C++
struct node {
	int l,r,dat,lazy;
} tree[1000001];

建树

用二分遍历每一个节点,将节点的 ll 和 rr 赋值。

C++
void tree_build(int left,int right,int pos) {
	tree[pos].l=left;\\赋值
	tree[pos].r=right;
	if(left==right)return;
	tree_build(left,(left+right)/2,pos*2+1);\\二分
	tree_build((left+right)/2+1,right,pos*2+2);
}

使用

对于需要修改的区间 [left,right][left,right],可以先判断该节点所表示的区间是否被包含,若包含,则直接修改值,并打上 lazy tag,否则继续寻找左子树和右子树。

C++
\\该代码实现了在一个区间内的所有值增加s1
void tree_plus(int left,int right,int pos) {
	if(tree[pos].l>=left&&tree[pos].r<=right) {\\在区间内
		int k=tree[pos].r-tree[pos].l+1;
		tree[pos].dat+=k*s1;\\修改值
		tree[pos].lazy+=s1;\\打上lazy tag
		return;
	}
	if(tree[pos].l==tree[pos].r)return;
	int mid=(tree[pos].l+tree[pos].r)/2;\\二分
	if(mid>=left)tree_plus(left,right,pos*2+1);\\左子节点
	if(mid<=right)tree_plus(left,right,pos*2+2);\\右子节点
	tree[pos].dat=tree[pos*2+1].dat+tree[pos*2+2].dat;\\结果为左右子树的值之和
}

下传

需要查找某个带 lazy tag 的节点的左右子节点之前,应对先下传 lazy tag,修改他们的值,并将该节点的 lazy tag 删除。

C++
if(tree[pos].lazy!=0) {\\有lazy tag时
    int k=tree[pos].lazy;
    int kk=tree[pos*2+1].r-tree[pos*2+1].l+1;
    tree[pos*2+1].dat+=k*kk;\\修改值
    tree[pos*2+1].lazy+=k;\\下传lazy tag
    kk=tree[pos*2+2].r-tree[pos*2+2].l+1;
    tree[pos*2+2].dat+=k*kk;\\修改值
    tree[pos*2+2].lazy+=k;\\下传lazy tag
    tree[pos].lazy=0;\\删除lazy tag
}

lazy tag 的代码

此处为P3372的 AC 代码

C++
#include<iostream>
using namespace std;
struct node {
	long long l,r,dat,lazy;
} tree[1000001];
long long x,l,r,s1;
int n,m;
void tree_plus(int left,int right,int pos) {\\区间增加值
	if(tree[pos].l>=left&&tree[pos].r<=right) {
		int k=tree[pos].r-tree[pos].l+1;
		tree[pos].dat+=k*s1;
		tree[pos].lazy+=s1;
		return;
	}
	if(tree[pos].lazy!=0) {\\下传 lazy tag
		int k=tree[pos].lazy;
		int kk=tree[pos*2+1].r-tree[pos*2+1].l+1;
		tree[pos*2+1].dat+=k*kk;
		tree[pos*2+1].lazy+=k;
		kk=tree[pos*2+2].r-tree[pos*2+2].l+1;
		tree[pos*2+2].dat+=k*kk;
		tree[pos*2+2].lazy+=k;
		tree[pos].lazy=0;
	}
	if(tree[pos].l==tree[pos].r)return;
	int mid=(tree[pos].l+tree[pos].r)/2;
	if(mid>=left)tree_plus(left,right,pos*2+1);
	if(mid<=right)tree_plus(left,right,pos*2+2);
	tree[pos].dat=tree[pos*2+1].dat+tree[pos*2+2].dat;
}
void tree_sum(int left,int right,int pos) {\\区间求和,与 tree_plus 类似
	if(tree[pos].l>=left&&tree[pos].r<=right) {
		s1+=tree[pos].dat;
		return;
	}
	if(tree[pos].lazy!=0) {
		int k=tree[pos].lazy;
		int kk=tree[pos*2+1].r-tree[pos*2+1].l+1;
		tree[pos*2+1].dat+=k*kk;
		tree[pos*2+1].lazy+=k;
		kk=tree[pos*2+2].r-tree[pos*2+2].l+1;
		tree[pos*2+2].dat+=k*kk;
		tree[pos*2+2].lazy+=k;
		tree[pos].lazy=0;
	}
	if(tree[pos].l==tree[pos].r)return;
	int mid=(tree[pos].l+tree[pos].r)/2;
	if(mid>=left)tree_sum(left,right,pos*2+1);
	if(mid<=right)tree_sum(left,right,pos*2+2);
}
void tree_build(int left,int right,int pos) {\\建树
	tree[pos].l=left;
	tree[pos].r=right;
	if(left==right)return;
	tree_build(left,(left+right)/2,pos*2+1);
	tree_build((left+right)/2+1,right,pos*2+2);
}
int main() {
	cin>>m>>n;
	tree_build(1,m,1);
	for(int i=1; i<=m; i++) {
		cin>>s1;
		tree_plus(i,i,1);
	}
	for(int i=1; i<=n; i++) {
		cin>>x;
		if(x==1) {
			cin>>l>>r>>s1;
			tree_plus(l,r,1);
		} else {
			cin>>l>>r;
			s1=0;
			tree_sum(l,r,1);
			cout<<s1<<endl;
		}
	}
}
原文