跳到正文

个人记录

2026/7/13下午 区间倍增总结

xuyulan约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月13日
收录
2026年7月14日
更新
2026年11月8日
篇幅
约 6 分钟

版本

  1. v1当前

P3865 【模板】ST 表 & RMQ 问题

题面

时间复杂度:O(nlogn)O(n log n)

转换方式:st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);

ST表是一种专门解决静态区间最值查询(RMQ) 问题的数据结构。

它的核心是利用倍增思想和DP递推:预先计算并存储所有以任意位置为起点、长度为 (2^j) 的区间最值(即 (st[i][j])),转移方程是 (st[i][j] = max(st[i][j-1], st[i+2^{j-1}][j-1])),预处理时间复杂度为 (O(n\log n))。

在查询任意区间 ([l, r]) 时,只需计算出区间长度的最高位 (k = \lfloor \log_2(r-l+1) \rfloor),然后将该区间“重叠”地拆分成两个长度为 (2^k) 的已预处理区间,直接取最大值即可,单次查询时间复杂度仅为 (O(1))。

C++
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e6+50;
int n,m,ans,a[N],st[N][22],lg[N];
int query(int l,int r){
	int k=lg[r-l+1];
	return max(st[l][k],st[r-(1<<k)+1][k]);
}
void init(){
	lg[1]=0;
	for(int i=2;i<=n;i++){
		lg[i]=lg[i/2]+1;
	}
	for(int j=1;j<=20;j++){
		for(int i=1;i+(1<<(j-1))-1<=n;i++){
			st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		st[i][0]=a[i];
	}
	init();
	while (m--){
		int l,r;
		cin>>l>>r;
		cout<<query(l,r)<<"\n";
	}
	return 0;
}

P1890 gcd 区间

题面

把选最大值的部分改为取最大公因数

C++
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e6+50;
int n,m,ans,a[N],st[N][22],lg[N];
int query(int l,int r){
	int k=lg[r-l+1];
	return __gcd(st[l][k],st[r-(1<<k)+1][k]);
}
void init(){
	lg[1]=0;
	for(int i=2;i<=n;i++){
		lg[i]=lg[i/2]+1;
	}
	for(int j=1;j<=20;j++){
		for(int i=1;i+(1<<(j-1))-1<=n;i++){
			st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]);
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		st[i][0]=a[i];
	}
	init();
	while (m--){
		int l,r;
		cin>>l>>r;
		cout<<query(l,r)<<"\n";
	}
	return 0;
}

P1198 [JSOI2008] 最大数

题面

代码的核心是维护一个逆向(或动态)ST 表。

  • 状态定义:st[i][j] 表示以位置 i 为右端点,向左延伸长度为 2^j 的区间内的最大值。这是一种“逆”ST表。
  • 动态更新:每当在数列尾部插入一个新元素 x 时,只更新以新元素为右端点的那些区间。其递推公式为: st[c][j] = max(st[c][j-1], st[c - (1 << (j-1))][j-1]) 这行代码即实现了从长度为 2^(j-1) 的区间合并得到长度为 2^j 的区间。
  • 查询:query(l, r) 函数利用经典的 ST 表查询方法,将区间 [l, r] 用两个已预处理的重叠区间覆盖,以 O(1) 的时间返回最大值。
C++
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=2e5+5;
int m,n,ans,cnt,a[N],st[N][65],lg[N];
int query(int l,int r){
	int k=lg[r-l+1];
	return max(st[l][k],st[r-(1<<k)+1][k]);
}
void init(){
	lg[0]=-1;
	for(int i=1;i<=N;i++){
		lg[i]=lg[i/2]+1;
	}
	return ;
}
void ch(int x){
	st[cnt][0]=x;
	for(int j=1;j<=20;j++){
		if(cnt>=(1<<j)){
			int c=cnt-(1<<j)+1;
			st[c][j]=max(st[c][j-1],st[c+(1<<(j-1))][j-1]);
		}else break;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	init();
	while (n--){
		char a;
		int x;
		cin>>a>>x;
		if(a=='Q'){
			ans=query(cnt-x+1,cnt);
			cout<<ans<<"\n";
		}else{
			x+=ans;
			x%=m;
			cnt++;
			ch(x);
		}
	}
	return 0;
}
原文