算法模板-线段树+懒标记

视频连接:C02【模板】线段树+懒标记 Luogu P3372 线段树 1_哔哩哔哩_bilibili

题目链接:P3372 【模板】线段树 1 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

P3374 【模板】树状数组 1 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

算法思路

递归建树

每个节点存储一段区间的左右端点以及区间的某种属性。通过递归建树。

点修改

点修改可以算是区间修改的一个特例。

区间查询

区间查询:主要的思路是拼凑与拆分。

区间修改

在区间修改当中加入了懒标记以增加效率,它的作用是当找到一个小区间被所求区间覆盖的话,就不再往下进行递归查找,只对当前小区间进行标记。等到下次查询或更新的时候再对小区间中的节点进行更新。

所以懒标记会在对小区间的查询或修改的时候率先更新,这体现在更新区间和查询区间的函数中,在不覆盖的情况下,会率先调用push_down函数对子节点进行更新。

算法模板

#include<bits/stdc++.h>
using namespace std;
#define lc p<<1
#define rc p<<1|1
#define N 100010
typedef long long ll;
ll n,w[N];

struct node{
	ll l,r,sum,add;
}tr[N*4];
void pushup(int p){
	tr[p].sum = tr[lc].sum + tr[rc].sum;
} 
void pushdown(int p){
	if(tr[p].add){
		tr[lc].sum+=tr[p].add*(tr[lc].r-tr[lc].l+1);
		tr[rc].sum+=tr[p].add*(tr[rc].r-tr[rc].l+1);
		tr[lc].add+=tr[p].add;
		tr[rc].add+=tr[p].add;
		tr[p].add = 0;
	}
} 
void build(int p,int l,int r){
	tr[p]={l,r,w[l],0};
	if(l==r) return;
	int m=l+r>>1;
	build(lc,l,m);
	build(rc,m+1,r);
	pushup(p); 
}
void update(int p,int x,int y,int k){
	if(x<=tr[p].l&&tr[p].r<=y){
		tr[p].sum+=(tr[p].r-tr[p].l+1)*k;
		tr[p].add+=k;
		return;
	}
	int m = tr[p].l+tr[p].r>>1;
	pushdown(p);
	if(x<=m) update(lc,x,y,k);
	if(y>m) update(rc,x,y,k);
	pushup(p); 
}
int query(int p,int x,int y){
	if(x<=tr[p].l&&tr[p].r<=y){
		return tr[p].sum;
	}
	int m = tr[p].l+tr[p].r>>1;
	pushdown(p);
	int sum = 0;
	if(x<=m) sum+=query(lc,x,y);
	if(y>m) sum+=query(rc,x,y);
	return sum;
}
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>w[i];
	build(1,1,n);
	while(m--){
		ll a,b,c,k;
		cin>>a>>b>>c;
		if(a==1){
			cin>>k;
			update(1,b,c,k);
		}else{
			cout<<query(1,b,c)<<endl;
		}
	}
	return 0;
	
} 
相关推荐
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂1 分钟前
实验4 循环结构
c语言·算法·基础题
新晓·故知26 分钟前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表
总裁余(余登武)37 分钟前
算法竞赛(Python)-万变中的不变“随机算法”
开发语言·python·算法
Eric.Lee20211 小时前
音频文件重采样 - python 实现
人工智能·python·深度学习·算法·audio·音频重采样
huapiaoy1 小时前
Redis中数据类型的使用(hash和list)
redis·算法·哈希算法
冷白白1 小时前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
鹤上听雷2 小时前
【AGC005D】~K Perm Counting(计数抽象成图)
算法
一叶祇秋2 小时前
Leetcode - 周赛417
算法·leetcode·职场和发展
武昌库里写JAVA2 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g2 小时前
GESP C++四级样题卷
java·c++·算法