线段树模板(cpp)

该文章介绍了线段树的数据结构,包括其基本构成如info和tag结构体,以及线段树的pushup、pushdown、build、modify和query等关键操作。文章通过C++代码展示了如何在线段树中进行区间加法、更新和查询等操作,并处理懒惰标签来优化效率。
摘要由CSDN通过智能技术生成

这个模板修改起来比较简单

// 线段树的信息
const int N = 2e5 + 10,mod = 1e9 + 7;
int a[N];
struct info // 存储线段树的值
{
	int size;
	int num;
};

struct tag // 存储线段树的懒标记
{
	int add;
	int mul;
};

struct Node // 线段树
{
	info val;
	tag lazy;
}tr[N << 2];
int st_size; // 线段树的总区间大小

// 线段树的具体操作
info operator + (const info &l,const info &r) // pushup的操作
{
	info c;
	c.size = l.size + r.size;
	c.num = (1ll * l.num + r.num) % mod;
	return c;
}

info operator + (const info &v,const tag &t) // pushdown时,对子节点info的操作
{
	info c;
	c.size = v.size;
	c.num = (1ll * v.num * t.mul + 1ll * v.size * t.add) % mod;
	return c;
}

tag operator + (const tag &ts,const tag &tp) // pushdown时,对子节点tag的操作
{
	tag c;
	c.add = (1ll * ts.add * tp.mul + tp.add) % mod;
	c.mul = 1ll * ts.mul * tp.mul % mod;
	return c;
}

void pushup(int u)
{
	tr[u].val = tr[u <&
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值