跳到正文

算法·理论

【数据结构】吉司机线段树(区间最值操作/区间历史最值)

CNS_5t0_0r2约 24 分钟
原文
文章信息与版本

文章信息

发布
2024年11月4日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 24 分钟

版本

  1. v1当前

请先阅读线段树一文再阅读此文。

在讲真正的区间最值操作/区间历史最值之前,先看一道练手题。

例题 1:Wall 砖墙

注意到本题不需要维护区间信息,所以每次操作直接打标记即可。

C++
#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 2e6 + 9,INF = 0x7fffffff;
int n,k;
struct node{
	int tag[2];
} t[N << 2];
bool in_range(int l,int r,int L,int R){
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){
	return l > R || r < L;
}
void make_tag(int root,int v,int type){
	if(type == 1){
		t[root].tag[0] = max(t[root].tag[0],v);
		t[root].tag[1] = max(t[root].tag[1],v);
	}
	else if(type == 2){
		t[root].tag[0] = min(t[root].tag[0],v);
		t[root].tag[1] = min(t[root].tag[1],v);
	}
}
void push_down(int root){
	make_tag(lchild,t[root].tag[0],1);
	make_tag(rchild,t[root].tag[0],1);
	make_tag(lchild,t[root].tag[1],2);
	make_tag(rchild,t[root].tag[1],2);
	t[root].tag[0] = 0;
	t[root].tag[1] = INF;
}
void build(int root,int l,int r){
	if(l == r){
		t[root].tag[0] = 0;
		t[root].tag[1] = INF;
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
}
void update(int root,int l,int r,int L,int R,int v,int type){
	if(out_range(l,r,L,R))
		return;
	if(in_range(l,r,L,R)){
		make_tag(root,v,type);
		return;
	}
	push_down(root);
	update(lchild,l,mid,L,R,v,type);
	update(rchild,mid + 1,r,L,R,v,type);
}
void print(int root,int l,int r){
	if(l == r){
		cout << t[root].tag[0] << '\n';
		return;
	}
	push_down(root);
	print(lchild,l,mid);
	print(rchild,mid + 1,r);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	while(k--){
		int op,l,r,k;
		cin >> op >> l >> r >> k;
		l++;r++;
		update(1,1,n,l,r,k,op);
	}
	print(1,1,n);
	return 0;
}

【1】区间最值操作

例题 2:最佳女选手

C++
#include<bits/stdc++.h>
#define int long long
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 5e5 + 9,INF = 1e18;

int n,m;
int a[N];

struct node{
	int sum,Max,Min;
	int se_max,se_min;
	int cnt_max,cnt_min;
	int tag_add,tag_max,tag_min;
} t[N << 2];

int len(int l,int r){
	return r - l + 1;
}

bool in_range(int l,int r,int L,int R){
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){
	return l > R || r < L;
}

void push_up(int root){
	t[root].sum = t[lchild].sum + t[rchild].sum;
	t[root].Max = max(t[lchild].Max,t[rchild].Max);
	t[root].Min = min(t[lchild].Min,t[rchild].Min);

	if(t[lchild].Max == t[rchild].Max){
		t[root].se_max = max(t[lchild].se_max,t[rchild].se_max);
		t[root].cnt_max = t[lchild].cnt_max + t[rchild].cnt_max;
	}
	else if(t[lchild].Max > t[rchild].Max){
		t[root].se_max = max(t[lchild].se_max,t[rchild].Max);
		t[root].cnt_max = t[lchild].cnt_max;
	}
	else{
		t[root].se_max = max(t[lchild].Max,t[rchild].se_max);
		t[root].cnt_max = t[rchild].cnt_max;
	}

	if(t[lchild].Min == t[rchild].Min){
		t[root].se_min = min(t[lchild].se_min,t[rchild].se_min);
		t[root].cnt_min = t[lchild].cnt_min + t[rchild].cnt_min;
	}
	else if(t[lchild].Min < t[rchild].Min){
		t[root].se_min = min(t[lchild].se_min,t[rchild].Min);
		t[root].cnt_min = t[lchild].cnt_min;
	}
	else{
		t[root].se_min = min(t[lchild].Min,t[rchild].se_min);
		t[root].cnt_min = t[rchild].cnt_min;
	}
}

void make_tag_add(int root,int len,int v){
	t[root].sum += len * v;
	t[root].Max += v;t[root].Min += v;
	if(t[root].se_max != -INF)
		t[root].se_max += v;
	if(t[root].se_min != INF)
		t[root].se_min += v;
	if(t[root].tag_max != -INF)
		t[root].tag_max += v;
	if(t[root].tag_min != INF)
		t[root].tag_min += v;
	t[root].tag_add += v;
}
void make_tag_max(int root,int v){
	if(t[root].Min > v)
		return;
	t[root].sum += (v - t[root].Min) * t[root].cnt_min;
	if(t[root].se_max == t[root].Min)
		t[root].se_max = v;
	if(t[root].Max == t[root].Min)
		t[root].Max = v;
	t[root].tag_min = max(t[root].tag_min,v);
	t[root].Min = v;t[root].tag_max = v;
}
void make_tag_min(int root,int v){
	if(t[root].Max <= v)
		return;
	t[root].sum += (v - t[root].Max) * t[root].cnt_max;
	if(t[root].se_min == t[root].Max)
		t[root].se_min = v;
	if(t[root].Min == t[root].Max)
		t[root].Min = v;
	t[root].tag_max = min(t[root].tag_max,v);
	t[root].Max = v;t[root].tag_min = v;
}

void push_down(int root,int l,int r){
	if(t[root].tag_add){
		make_tag_add(lchild,len(l,mid),t[root].tag_add);
		make_tag_add(rchild,len(mid + 1,r),t[root].tag_add);
	}
	if(t[root].tag_max != -INF){
		make_tag_max(lchild,t[root].tag_max);
		make_tag_max(rchild,t[root].tag_max);
	}
	if(t[root].tag_min != INF){
		make_tag_min(lchild,t[root].tag_min);
		make_tag_min(rchild,t[root].tag_min);
	}
	t[root].tag_add = 0;t[root].tag_max = -INF;t[root].tag_min = INF;
}

void build(int root,int l,int r){
	t[root].tag_max = -INF;t[root].tag_min = INF;
	if(l == r){
		t[root].sum = t[root].Max = t[root].Min = a[l];
		t[root].se_max = t[root].tag_max = -INF;t[root].se_min = INF;
		t[root].cnt_max = t[root].cnt_min = 1;
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
	push_up(root);
}

void update_sum(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R))
		return;
	if(in_range(l,r,L,R)){
		make_tag_add(root,len(l,r),v);
		return;
	}
	push_down(root,l,r);
	update_sum(lchild,l,mid,L,R,v);
	update_sum(rchild,mid + 1,r,L,R,v);
	push_up(root);
}
void update_max(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R) || v <= t[root].Min)
		return;
	if(in_range(l,r,L,R) && t[root].se_min > v){
		make_tag_max(root,v);
		return;
	}
	push_down(root,l,r);
	update_max(lchild,l,mid,L,R,v);
	update_max(rchild,mid + 1,r,L,R,v);
	push_up(root);
}
void update_min(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R) || v >= t[root].Max)
		return;
	if(in_range(l,r,L,R) && t[root].se_max < v){
		make_tag_min(root,v);
		return;
	}
	push_down(root,l,r);
	update_min(lchild,l,mid,L,R,v);
	update_min(rchild,mid + 1,r,L,R,v);
	push_up(root);
}

int query_sum(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return 0;
	if(in_range(l,r,L,R))
		return t[root].sum;
	push_down(root,l,r);
	return query_sum(lchild,l,mid,L,R) + query_sum(rchild,mid + 1,r,L,R);
}
int query_max(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return -INF;
	if(in_range(l,r,L,R))
		return t[root].Max;
	push_down(root,l,r);
	return max(query_max(lchild,l,mid,L,R),query_max(rchild,mid + 1,r,L,R));
}
int query_min(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return INF;
	if(in_range(l,r,L,R))
		return t[root].Min;
	push_down(root,l,r);
	return min(query_min(lchild,l,mid,L,R),query_min(rchild,mid + 1,r,L,R));
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	build(1,1,n);
	cin >> m;
	while(m--){
		int op,l,r,v;
		cin >> op >> l >> r;
		if(op <= 3)
			cin >> v;
		if(op == 1)
			update_sum(1,1,n,l,r,v);
		else if(op == 2)
			update_max(1,1,n,l,r,v);
		else if(op == 3)
			update_min(1,1,n,l,r,v);
		else if(op == 4)
			cout << query_sum(1,1,n,l,r) << '\n';
		else if(op == 5)
			cout << query_max(1,1,n,l,r) << '\n';
		else if(op == 6)
			cout << query_min(1,1,n,l,r) << '\n';
	}
	return 0;
}

【2】区间历史最值

以下是区间历史最值模版的代码:

C++
#include<bits/stdc++.h>
#define int long long
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 5e5 + 9,INF = 0x3f3f3f3f3f3f3f;
int n,m;
int a[N];
struct node{
	int sum;
	int max_a,cnt;
	int max_b;
	int se_max;
	int tag1,tag2,tag3,tag4;
} t[N << 2];

int len(int l,int r){
	return r - l + 1;
}

bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集
	return l > R || r < L;
}

void push_up(int root){
	t[root].sum = t[lchild].sum + t[rchild].sum;
	t[root].max_a = max(t[lchild].max_a,t[rchild].max_a);
	t[root].max_b = max(t[lchild].max_b,t[rchild].max_b);
	if(t[lchild].max_a == t[rchild].max_a){
		t[root].se_max = max(t[lchild].se_max,t[rchild].se_max);
		t[root].cnt = t[lchild].cnt + t[rchild].cnt;
	}
	else if(t[lchild].max_a > t[rchild].max_a){
		t[root].se_max = max(t[lchild].se_max,t[rchild].max_a);
		t[root].cnt = t[lchild].cnt;
	}
	else{
		t[root].se_max = max(t[lchild].max_a,t[rchild].se_max);
		t[root].cnt = t[rchild].cnt;
	}
}

void make_tag(int root,int len,int tag1,int tag2,int tag3,int tag4){
	t[root].sum += tag1 * t[root].cnt + tag2 * (len - t[root].cnt);
	t[root].max_b = max(t[root].max_b,t[root].max_a + tag3);
	t[root].max_a += tag1;
	if(t[root].se_max != -INF)
		t[root].se_max += tag2;
	t[root].tag3 = max(t[root].tag3,t[root].tag1 + tag3);	t[root].tag4 = max(t[root].tag4,t[root].tag2 + tag4);
	t[root].tag1 += tag1;									t[root].tag2 += tag2;
}
void push_down(int root,int l,int r){
	int Max = max(t[lchild].max_a,t[rchild].max_a);

	if(t[lchild].max_a == Max)
		make_tag(lchild,len(l,mid),t[root].tag1,t[root].tag2,t[root].tag3,t[root].tag4);
	else
		make_tag(lchild,len(l,mid),t[root].tag2,t[root].tag2,t[root].tag4,t[root].tag4);

	if(t[rchild].max_a == Max)
		make_tag(rchild,len(mid + 1,r),t[root].tag1,t[root].tag2,t[root].tag3,t[root].tag4);
	else
		make_tag(rchild,len(mid + 1,r),t[root].tag2,t[root].tag2,t[root].tag4,t[root].tag4);

	t[root].tag1 = t[root].tag2 = t[root].tag3 = t[root].tag4 = 0;
}

void build(int root,int l,int r){
	if(l == r){
		t[root].sum = t[root].max_a = t[root].max_b = a[l];
		t[root].cnt = 1;t[root].se_max = -INF;
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
	push_up(root);
}

void update1(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R))
		return;
	if(in_range(l,r,L,R)){
		t[root].sum += v * len(l,r);
		t[root].max_a += v;
		t[root].max_b = max(t[root].max_b,t[root].max_a);
		if(t[root].se_max != -INF)
			t[root].se_max += v;
		t[root].tag1 += v;								t[root].tag2 += v;
		t[root].tag3 = max(t[root].tag3,t[root].tag1);	t[root].tag4 = max(t[root].tag4,t[root].tag2);
		return;
	}
	push_down(root,l,r);
	update1(lchild,l,mid,L,R,v);
	update1(rchild,mid + 1,r,L,R,v);
	push_up(root);
}
void update2(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R) || v >= t[root].max_a)
		return;
	if(in_range(l,r,L,R) && t[root].se_max < v){
		int tmp = t[root].max_a - v;
		t[root].sum -= t[root].cnt * tmp;
		t[root].max_a = v;t[root].tag1 -= tmp;
		return;
	}
	push_down(root,l,r);
	update2(lchild,l,mid,L,R,v);
	update2(rchild,mid + 1,r,L,R,v);
	push_up(root);
}

int query1(int root,int l,int r,int L,int R){//区间和
	if(out_range(l,r,L,R))
		return 0;
	if(in_range(l,r,L,R))
		return t[root].sum;
	push_down(root,l,r);
	return query1(lchild,l,mid,L,R) + query1(rchild,mid + 1,r,L,R);
}

int query2(int root,int l,int r,int L,int R){//区间最值
	if(out_range(l,r,L,R))
		return -INF;
	if(in_range(l,r,L,R))
		return t[root].max_a;
	push_down(root,l,r);
	return max(query2(lchild,l,mid,L,R),query2(rchild,mid + 1,r,L,R));
}

int query3(int root,int l,int r,int L,int R){//区间历史最值
	if(out_range(l,r,L,R))
		return -INF;
	if(in_range(l,r,L,R))
		return t[root].max_b;
	push_down(root,l,r);
	return max(query3(lchild,l,mid,L,R),query3(rchild,mid + 1,r,L,R));
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	build(1,1,n);
	while(m--){
		int op,l,r,v;
		cin >> op >> l >> r;
		if(op <= 2)
			cin >> v;
		if(op == 1)
			update1(1,1,n,l,r,v);
		else if(op == 2)
			update2(1,1,n,l,r,v);
		else if(op == 3)
			cout << query1(1,1,n,l,r) << '\n';
		else if(op == 4)
			cout << query2(1,1,n,l,r) << '\n';
		else if(op == 5)
			cout << query3(1,1,n,l,r) << '\n';
	}
	return 0;
}
原文