首页 > 其他分享 >#P1042. 静态RMQ[ST表模板]

#P1042. 静态RMQ[ST表模板]

时间:2023-11-26 21:45:16浏览次数:25  
标签:RMQ int max long ST P1042 init

题意是:给定一个长度为 N 的数列,和 M 次询问,求出每一次询问的区间内数字的最大值。

ST表的基本功能是对区间进行查询,其核心使用的是倍增的思想

f[i][k]:意思是从第i个数开始往后2^k个数

f[i][k]=max(f[i][k-1],f[i+2^k-1][k-1])

求【l,r】区间 max(f[i][k],f[r-2^k+1][k])

#define int long long
using namespace std;
const int N=1e5+10;
int a[N];
int f[N][30];
int n,m;
void init(){
	for(int i=1;i<=n;i++){
		f[i][0]=a[i];
	}
	for(int k=1;k<=20;k++){
		for(int i=1;i+(1<<k-1)<=n;i++){
			f[i][k]=max(f[i][k-1],f[i+(1<<k-1)][k-1]);
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	init();
	for(int i=1;i<=m;i++){
		int l,r;
		cin>>l>>r;
		int len=r-l+1;
		int k=log2(len);
		int ans=max(f[l][k],f[r-(1<<k)+1][k]);
		cout<<ans<<"\n";
	}
	return 0;
} 

标签:RMQ,int,max,long,ST,P1042,init
From: https://www.cnblogs.com/yufan1102/p/17858021.html

相关文章

  • 在visual studio反汇编得出的函数之间的一些管旭
    非裸函数执行过程002018D1push3002018D3push2002018D5push1//将三个数压入栈中002018D7callstd::basic_ostreamchar,std::char_traits<char>::sentry::sentry(0201497h)002018DCaddesp,0Ch//ebp表示栈底,esp表示栈顶,前面三个数将esp减去了0x0C,现在需要加上0......
  • AtCoder Beginner Contest 326
    A-2UP3DOWN#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){inta,b;cin>>a>>b;if(a<bandb-a<=2)cout<<"Yes\n";elseif(a>banda......
  • SD Host控制器微架构设计
    微架构设计思路ahb_slave_if中的寄存器可以在datasheet中进行描述sd_clk-时钟产生模块的接口描述sd_data_fsm和sd_cmd_fsm-状态机描述发送时序需要遵守,并且在发送的时候需要产生CRC接受时序需要遵守,并且要接收CRC,进行比较FiFo中有存储体存储数据,FiFo_ctrl模块进行......
  • AtCoder Beginner Contest 322
    A-FirstABC2#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong#definempmake_pairusingvi=vector<int>;usingpii=pair<int,int>;voidsolve(){intn;strings;cin>>n>>s;......
  • 使用xgboost的c接口推理模型
    title:使用xgboost的c接口推理模型banner_img:https://cdn.studyinglover.com/pic/2023/07/b5c4ecf9aa476ca1073f99b22fe9605e.jpgdate:2023-9-1021:10:00categories:-踩坑tags:-机器学习使用xgboost的c接口推理模型官方capitutorial和文档,非常恶心的一点是,tutor......
  • Reference and inspiration from China's strategy for addressing water pollution i
     AccordingtoChina'sthreelineonepermitmeasures,webelievethatthishasacertainreferencevalueforwaterpollutionissuesinAfrica.The"threelines"referstotheecologicalprotectionredline,theenvironmentalqualitybottom......
  • docker搭建elasticsearch并使用python连接
    title:docker搭建elasticsearch并使用python连接banner_img:https://cdn.studyinglover.com/pic/2023/10/0863cb015e8d69fbce68ebe57bea96d8.jpgdate:2023-10-921:48:00categories:-踩坑docker搭建elasticsearch并使用python连接搭建创建一个docker网络dockernetwo......
  • ggml教程|mnist手写体识别量化推理
    title:ggml教程|mnist手写体识别量化推理banner_img:https://cdn.studyinglover.com/pic/2023/11/fa14d6dfd95fb9d38276a50a5519e2d2.webpdate:2023-11-1218:49:00ggml教程|mnist手写体识别量化推理MNIST手写体识别是经典的机器学习问题,可以被称作机器学习的helloworld......
  • StableDiffusion笔记
    title:StableDiffusion笔记banner_img:https://drive.studyinglover.com/api/raw/?path=/photos/blog/background/1679396994125.pngdate:2023-5-2915:36:00categories:-笔记tags:-文字生成图片StableDiffusion是一个图像生成方法,由 StabilityAI and Runway......
  • Paper Gestalt笔记
    title:PaperGestalt笔记banner_img:https://cdn.studyinglover.com/pic/2023/07/5deff473fdf93539d3952d3d6894add3.pngdate:2023-7-2710:57:00PaperGestalt笔记最近读到了一篇CVPR2010非常优秀的论文,叫做PaperGestalt,他考虑到近年来(2010年的近年来)CVPR的投稿两......