【基本算法】倍增与ST表

目录

前言

思想

ST表  

质量检测 

忠诚 

海底高铁


前言

ST表基于倍增思想,可以做到O(nlogn)预处理,O(1)回答每个询问。但是不支持修改操作。基于倍增思想,可以发现我们能使用至多两个预处理过的区间来覆盖询问区间,也就是说询问时的时间复杂度可以被降至O(1),在处理大量询问的题目时十分有效。

思想

1预处理

令f[i][j]表示区间[i,i+2^j-1]的最大值

显然f[i,0]=a[i]

根据定义式,第二维相当于跳了2^j-1步,

写出状态转移方程:f[i][j]=max(f[i][j-1],f[i+2^(j-1)][j-1])

2查询

对于每个查询,我们可以把它分成两部分,[l,l+2^s-1]与[r-2^s+1,r]

最后答案就是max(f[l][s],f[r-(1<<s)+1][s]),其中s=Log[y-x+1]   (Log=log2)

ST表  

洛谷:ST表

// Problem: P3865 【模板】ST 表
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3865
// Memory Limit: 125 MB
// Time Limit: 800000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long ll;

const int N = 2000010;

int n,m;
int a[N],f[N][17],Log[N];

void pre(){
	Log[1]=0;
	Log[2]=1;
	for(int i=3;i<N;i++){
		Log[i]=Log[i/2]+1;
	}
}

void s(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>f[i][0];
	}	
	pre();
	for(int j=1;j<=21;j++){  //2^20=1e6
		for(int i=1;i+(1<<j)-1<=n;i++){  
			f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
		}
	}
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		int s=Log[y-x+1];
		cout<<max(f[x][s],f[y-(1<<s)+1][s])<<"\n";  //查询时分成两个有重合的区间,
	}
	

}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
	    s();
	}
	
	return 0;	

}

质量检测 

洛谷:质量检测

// Problem: P2251 质量检测
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2251
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

typedef long long ll;

const int N = 2000010;

int f[N][21],Log[N];

void pre(){
	Log[1]=0;
	Log[2]=1;
	for(int i=3;i<N;i++){
		Log[i]=Log[i/2]+1;
	}
}

void s(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>f[i][0];
	}
	pre();
	for(int j=1;j<=21;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
		}
	}
	
	int x=Log[m];
	for(int i=1;i<=n-m+1;i++){
		cout<<min(f[i][x],f[i+m-1-(1<<x)+1][x])<<"\n";
	}

}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
	    s();
	}
	return 0;	
}

忠诚 

洛谷:忠诚

// Problem: S - 忠诚
// Contest: Virtual Judge - 2023暑期训练-基本算法
// URL: https://vjudge.net/contest/568123#problem/S
// Memory Limit: 131 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

typedef long long ll;

const int N = 200010;

int n,m;
int f[N][21],Log[N];

void pre(){
	Log[1]=0;
	Log[2]=1;
	for(int i=3;i<N;i++){
		Log[i]=Log[i/2]+1;
	}
}

void s(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>f[i][0];
	}
	pre();
	for(int j=1;j<=21;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
		}
	}
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		int s=Log[y-x+1];
		cout<<min(f[x][s],f[y-(1<<s)+1][s])<<' ';
	}

}

int main(){
	int T=1;
	//cin>>T;
	while(T--){
	    s();
	}
	
	return 0;	

}

海底高铁

洛谷:海底高铁

// Problem: P3406 海底高铁
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3406
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
 
#include<bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
const int N = 2e5+5;
 
void s(){
    int n,m;
    cin>>n>>m;
    int p[N],t[N];
    for(int i=0;i<N;i++){
        t[i]=0;
    }
    for(int i=1;i<=m;i++){
        cin>>p[i];
    }
    
    int a[N],b[N],c[N]; 
    for(int i=1;i<=n-1;i++){
        cin>>a[i]>>b[i]>>c[i];
    }
    for(int i=1;i<=m-1;i++){
        int x=min(p[i],p[i+1]);
        int y=max(p[i],p[i+1]);
        t[x]++;
        t[y]--;
    }
    for(int i=2;i<=n;i++){
        t[i]+=t[i-1];
    }
    int res=0;
    for(int i=1;i<=n-1;i++){
        res+=min(a[i]*t[i],b[i]*t[i]+c[i]);
    }
    cout<<res<<"\n";
 
}
 
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--){
        s();
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值