本文主要是介绍刘汝佳--线段树模版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、线段树(点修改)
Update(x,v): 把Ax修改为v
Query(L,R): 计算区间[qL,qR] 最小值。
代码:
// Dynamic RMQ
// Rujia Liu
// 输入格式:
// n m 数组范围是a[1]~a[n],初始化为0。操作有m个
// 1 p v 表示设a[p]=v
// 2 L R 查询a[L]~a[R]的min
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;const int INF = 1000000000;
const int maxnode = 1<<17;int op, qL, qR, p, v; //qL和qR为全局变量,询问区间[qL,qR];struct IntervalTree {int minv[maxnode];void update(int o, int L, int R) {int M = L + (R-L)/2;if(L == R) minv[o] = v; // 叶结点,直接更新minvelse {// 先递归更新左子树或右子树if(p <= M) update(o*2, L, M); else update(o*2+1, M+1, R);// 然后计算本结点的minvminv[o] = min(minv[o*2], minv[o*2+1]);}}int query(int o, int L, int R) {int M = L + (R-L)/2, ans = INF;if(qL <= L && R <= qR) return minv[o]; // 当前结点完全包含在查询区间内if(qL <= M) ans = min(ans, query(o*2, L, M)); // 往左走if(M < qR) ans = min(ans, query(o*2+1, M+1, R)); // 往右走return ans;}
};IntervalTree tree;int main() {int n, m;while(scanf("%d%d", &n, &m) == 2) {memset(&tree, 0, sizeof(tree));while(m--) {scanf("%d", &op);if(op == 1) {scanf("%d%d", &p, &v);tree.update(1, 1, n); // 修改树节点,或者是建树的过程} else {scanf("%d%d", &qL, &qR); //修改询问区间printf("%d\n", tree.query(1, 1, n));}}}return 0;
}
二、区间修改:
1.操作一:
// Fast Sequence Operations I
// Rujia Liu
// 输入格式:
// n m 数组范围是a[1]~a[n],初始化为0。操作有m个
// 1 L R v 表示设a[L]+=v, a[L+1]+v, ..., a[R]+=v
// 2 L R 查询a[L]~a[R]的sum, min和max
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;const int maxnode = 1<<17;int _sum, _min, _max, op, qL, qR, v; //_sum为全局变量struct IntervalTree {int sumv[maxnode], minv[maxnode], maxv[maxnode], addv[maxnode];// 维护信息void maintain(int o, int L, int R) {int lc = o*2, rc = o*2+1;sumv[o] = minv[o] = maxv[o] = 0;if(R > L) {sumv[o] = sumv[lc] + sumv[rc];minv[o] = min(minv[lc], minv[rc]);maxv[o] = max(maxv[lc], maxv[rc]);}if(addv[o]) { minv[o] += addv[o]; maxv[o] += addv[o]; sumv[o] += addv[o] * (R-L+1); }}void update(int o, int L, int R) {int lc = o*2, rc = o*2+1;if(qL <= L && qR >= R) { // 递归边界 addv[o] += v; // 累加边界的add值} else {int M = L + (R-L)/2;if(qL <= M) update(lc, L, M);if(qR > M) update(rc, M+1, R);}maintain(o, L, R); // 递归结束前重新计算本结点的附加信息}void query(int o, int L, int R, int add) {if(qL <= L && qR >= R) { // 递归边界:用边界区间的附加信息更新答案_sum += sumv[o] + add * (R-L+1);_min = min(_min, minv[o] + add);_max = max(_max, maxv[o] + add);} else { // 递归统计,累加参数addint M = L + (R-L)/2;if(qL <= M) query(o*2, L, M, add + addv[o]);if(qR > M) query(o*2+1, M+1, R, add + addv[o]);}}
};const int INF = 1000000000;IntervalTree tree;int main() {int n, m;while(scanf("%d%d", &n, &m) == 2) {memset(&tree, 0, sizeof(tree));while(m--) {scanf("%d%d%d", &op, &qL, &qR);if(op == 1) {scanf("%d", &v);tree.update(1, 1, n);} else {_sum = 0; _min = INF; _max = -INF;tree.query(1, 1, n, 0);printf("%d %d %d\n", _sum, _min, _max);}}}return 0;
}
2.操作二:
// Fast Sequence Operations II
// Rujia Liu
// 输入格式:
// n m 数组范围是a[1]~a[n],初始化为0。操作有m个
// 1 L R v 表示设a[L]=a[L+1]=...=a[R] = v。其中v > 0
// 2 L R 查询a[L]~a[R]的sum, min和max
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;const int maxnode = 1<<17;int _sum, _min, _max, op, qL, qR, v;struct IntervalTree {int sumv[maxnode], minv[maxnode], maxv[maxnode], setv[maxnode];// 维护信息void maintain(int o, int L, int R) {int lc = o*2, rc = o*2+1;if(R > L) {sumv[o] = sumv[lc] + sumv[rc];minv[o] = min(minv[lc], minv[rc]);maxv[o] = max(maxv[lc], maxv[rc]);}if(setv[o] >= 0) { minv[o] = maxv[o] = setv[o]; sumv[o] = setv[o] * (R-L+1); }}// 标记传递void pushdown(int o) {int lc = o*2, rc = o*2+1;if(setv[o] >= 0) { //本结点有标记才传递。注意本题中set值非负,所以-1代表没有标记setv[lc] = setv[rc] = setv[o];setv[o] = -1; // 清除本结点标记}}void update(int o, int L, int R) {int lc = o*2, rc = o*2+1;if(qL <= L && qR >= R) { // 标记修改setv[o] = v;} else {pushdown(o);int M = L + (R-L)/2;if(qL <= M) update(lc, L, M); else maintain(lc, L, M);if(qR > M) update(rc, M+1, R); else maintain(rc, M+1, R);}maintain(o, L, R);}void query(int o, int L, int R) {if(setv[o] >= 0) { // 递归边界1:有set标记_sum += setv[o] * (min(R,qR)-max(L,qL)+1);_min = min(_min, setv[o]);_max = max(_max, setv[o]);} else if(qL <= L && qR >= R) { // 递归边界2:边界区间_sum += sumv[o]; // 此边界区间没有被任何set操作影响_min = min(_min, minv[o]);_max = max(_max, maxv[o]);} else { // 递归统计int M = L + (R-L)/2;if(qL <= M) query(o*2, L, M);if(qR > M) query(o*2+1, M+1, R);}}
};const int INF = 1000000000;IntervalTree tree;int main() {int n, m;while(scanf("%d%d", &n, &m) == 2) {memset(&tree, 0, sizeof(tree));memset(tree.setv, -1, sizeof(tree.setv));tree.setv[1] = 0;while(m--) {scanf("%d%d%d", &op, &qL, &qR);if(op == 1) {scanf("%d", &v);tree.update(1, 1, n);} else {_sum = 0; _min = INF; _max = -INF;tree.query(1, 1, n);printf("%d %d %d\n", _sum, _min, _max);}}}return 0;
}
这篇关于刘汝佳--线段树模版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!