跳到正文

个人记录

倍增基础-st总结

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

文章信息

发布
2025年7月15日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 7 分钟

版本

  1. v1当前

1.U519510 ١١(❛ᴗ❛)【倍增】-最小线段合并

思想跟下一题表很像,每次减去 2max2^{max} 拆分线段即可

Ac Code

C++
#include<bits/stdc++.h>
using namespace std;
int n;
int ans=0;
int get_max(int x){
	int cnt=1;
	if(x==1)return 1;
    //x>=cnt*2变式,防止爆int
	while(cnt<=x/2){
		cnt*=2;
	}
	return cnt;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
    //特判
	if(n==0){
		cout<<-1;
		return 0;
	}
    //n一定会减到0
	while(n){
		n-=get_max(n);
		ans++;//线段数增加
	}
	cout<<ans;
	return 0;
} 

本题运用了倍增的思想,把线段分为多个二次幂的子线段,我们也能将大问题分解为小问题,得出问题的答案


2.P3865 【模板】ST 表 && RMQ 问题

RMQ(RangeMinimumQuery)RMQ(Range Minimum Query) 即区间查询问题

本题 1≤N≤105,1≤M≤2×1061≤N≤10^5,1≤M≤2×10^6 数据较大,时间复杂度较高,以至于要用快读,暴力不行

我们可以用 stst 表,存子问题答案( O(nlog(n)O(nlog(n) ), 查询也只需 O(1)O(1) 的复杂度

Ac Code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int st[N][25];
//st[i][j]表示下标从i开始,长度为2^k的区间最大值
int lg[N];
int n,m;
void st_init(){
	lg[0]=-1;
	for(int i=1;i<=n;i++){
		lg[i]=lg[i/2]+1;
	}
	for(int len=1;(1<<len)<=n;len++){
		for(int i=1;i+(1<<len)-1<=n;i++){
			st[i][len]=max(st[i][len-1],st[i+(1<<(len-1))][len-1]);//存子问题最大值
		}
	}
	return ;
}
int qiery(int l,int r){
	int len=lg[r-l+1];
    //min,max,gcd,lcm求解区间合并,无重叠状态影响
	return max(st[l][len],st[r-(1<<len)+1][len]);
}
int main(){
    //快读
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>st[i][0];//初始为2^0
	}
	st_init();
	while(m--){
		int l,r;
		cin>>l>>r;
		cout<<qiery(l,r)<<'\n';//用\n
	}
	return 0;
} 

3.P1890 gcd区间

把 maxmax 换成 gcdgcd 即可

Ac Code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int st[N][25];
int lg[N];
int n,m;
void st_init(){
	lg[0]=-1;
	for(int i=1;i<=n;i++){
		lg[i]=lg[i/2]+1;
	}
	for(int len=1;(1<<len)<=n;len++){
		for(int i=1;i+(1<<len)-1<=n;i++){
            //本题未卡gcd,但建议手写一个
			st[i][len]=__gcd(st[i][len-1],st[i+(1<<(len-1))][len-1]);
		}
	}
	return ;
}
int qiery(int l,int r){
	int len=lg[r-l+1];
	return __gcd(st[l][len],st[r-(1<<len)+1][len]);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>st[i][0];
	}
	st_init();
	while(m--){
		int l,r;
		cin>>l>>r;
		cout<<qiery(l,r)<<'\n';
	}
	return 0;
} 

4.P1198 [JSOI2008] 最大数

若用原来方法,每次插入新元素,要重新初始化( 2020 分),要写一个 insertinsert 函数

插入后 st[len][i]=max(st[len][i−1],st[len+(1<<(i−1))][i−1])st[len][i]=max(st[len][i-1],st[len+(1<<(i-1))][i-1])

Ac Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+7;
int n,m,d;
int t=0;
int st[N][65];
int lg[N];
void st_init(){
	lg[0]=-1;
    //初始化时范围到N
	for(int i=1;i<=N;i++){
		lg[i]=lg[i/2]+1;
	}
	return ;
}
void st_insert(){
	for(int i=1;i<=20;i++){
		if(n-(1<<i)>=0){
			int len=n-(1<<i)+1;
			st[len][i]=max(st[len][i-1],st[len+(1<<(i-1))][i-1]);
		}
		else{
			break;
		}
	}
	return ;
} 
int qiery(int l,int r){
	int len=lg[r-l+1];
	return max(st[l][len],st[r-(1<<len)+1][len]);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>m>>d;
	st_init();//初始化
	while(m--){
		char s;
		int x;
		cin>>s>>x;
		if(s=='A'){
			x+=t;
			x%=d;
			st[++n][0]=x;
			st_insert();//插入
		}
		else{
			t=qiery(n-x+1,n);//存上次查询的答案
			cout<<t<<'\n';//查询
		}
	}
	return 0;
} 
原文