跳到正文

算法·理论

线段树模板2

_Ted_约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

对应题目:P3373

这是我的模板,供大家参考,也供我自己复习。

有一些要点切记:

  • 一定要检查清楚有没有开够long long,否则要见祖宗

  • 线段树4倍空间别忘

  • 应模尽模别忘

  • 卡常的玩意儿写着估计保险一点

怼着这篇题解写的

C++
#include<bits/stdc++.h>
#define ll long long
#define mul tree[k].mul_lzy
#define pls tree[k].pls_lzy
#define lson (k << 1)
#define rson (k << 1 | 1)

const int MAX = 100000;
int n, q;
ll a[MAX+5], z, mod;
int opt, x, y;

struct Segment_Tree{
	struct Tree{
		int l, r;//Segment's Left side & Right side.
		ll sum, pls_lzy, mul_lzy = 1;
		//Here needs two lazy tags:the tag for the plus operation,and the tag for the multiplication operation.
	}tree[MAX << 2];

	inline void push_down(int k) {
		/*
		the function of push_down
		Step 1:Push down the father's lazy tags to his sons' sum.
			We do multiplication first,then plus.
		Step 2:Push down the father's mul_lzy to his sons' mul_lzy.
			It's easy.
		Step 3:Push down the father's pls_lzy to his sons' pls_lzy.
			We must make sure the sons' pls_lzy get multiplicated,then plus.
		*/
		tree[lson].sum = (tree[lson].sum * mul % mod + pls * (tree[lson].r - tree[lson].l + 1) % mod) % mod;
		tree[rson].sum = (tree[rson].sum * mul % mod + pls * (tree[rson].r - tree[rson].l + 1) % mod) % mod;
		tree[lson].mul_lzy = tree[lson].mul_lzy * mul % mod;
		tree[rson].mul_lzy = tree[rson].mul_lzy * mul % mod;
		tree[lson].pls_lzy = (tree[lson].pls_lzy * mul % mod + pls) % mod;
		tree[rson].pls_lzy = (tree[rson].pls_lzy * mul % mod + pls) % mod;
		pls = 0, mul = 1;//Don't forget to make sure the father's lazy tags get set.
	}

	inline void build(int k, int l, int r) {//It's easy.
		tree[k].l = l, tree[k].r = r;
		if(l == r) {
			tree[k].sum = a[l] % mod;
			return ;
		}
		int mid = l + r >> 1;
		build(lson, l, mid);
		build(rson, mid + 1, r);
		tree[k].sum = (tree[lson].sum + tree[rson].sum) % mod;
	}
	//Here we also need two Update functions,one for plus operation,and another for multiplication operation.
	inline void pls_update(int k, int l, int r, ll num) {
	//It's almost the same as  the first Segment_Tree template's update function.
		if(tree[k].l >= l && tree[k].r <= r) {
			tree[k].sum = (tree[k].sum + num * (tree[k].r - tree[k].l + 1) % mod) % mod;
			pls = (pls + num) % mod;
			return;
		}
		push_down(k);
		if(l <= tree[lson].r) pls_update(lson, l, r, num);
		if(r >= tree[rson].l) pls_update(rson, l, r, num);
		tree[k].sum = (tree[lson].sum + tree[rson].sum) % mod;
	}

	inline void mul_update(int k, int l, int r, ll num) {
		//Not so different from the plus_update function.
		if(tree[k].l >= l && tree[k].r <= r) {
			tree[k].sum = tree[k].sum * num % mod;
			mul = mul * num % mod;
			pls = pls * num % mod;//Don't forget to make sure the pls_tag to be multiplicated.
			return;
		}
		push_down(k);
		if(l <= tree[lson].r) mul_update(lson, l, r, num);
		if(r >= tree[rson].l) mul_update(rson, l, r, num);
		tree[k].sum  = (tree[lson].sum + tree[rson].sum) % mod;
	}

	inline ll query(int k, int l, int r) {//It's easy.
		if(tree[k].l >= l && tree[k].r <= r) return tree[k].sum;
		push_down(k);
		ll sum = 0;
		if(l <= tree[lson].r) sum = (sum + query(lson, l, r)) % mod;
		if(r >= tree[rson].l) sum = (sum + query(rson, l, r)) % mod;
		return sum;
	}
}S_Tree;
int main() {
	std::cin >> n >> q >> mod;
	for(int i = 1; i <= n; i ++) std::cin >> a[i];
	S_Tree.build(1, 1, n);
	while(q --> 0) {
		std::cin >> opt >> x >> y;
		if(opt == 1) {
			std::cin >> z;
			S_Tree.mul_update(1, x, y, z);
		}
		else if(opt == 2) {
			std::cin >> z;
			S_Tree.pls_update(1, x, y, z);
		}
		else {
			std::cout << S_Tree.query(1, x, y) <<'\n';
		}
	}
	return 0;
}
原文