首页 > 其他分享 >【树论】RMQ问题和ST表

【树论】RMQ问题和ST表

时间:2023-08-24 14:46:17浏览次数:31  
标签:ch int 复杂度 树论 ST while RMQ getchar

目录

RMQ问题

RMQ(Range Maximum/Minimum Query)问题,即区间最值问题。

一般是多次询问,对时间复杂度要求高,一般需要 \(O(logn)\) 或 \(O(1)\) 复杂度

ST表

p[i][j] 是以i为起点,连续 \(2^j\) 个数字的最大值(是一个递推表)

3 9 4 2 1 6 8 5
p[i][0] 3 9 4 2 1 6 8 5
p[i][1] 9 9 4 2 6 8 8 -
p[i][2] 9 9 6 8 8 - - -
p[i][3] 9 - - - - - - -

优缺点

优点:可查询,可在末尾插入删除
缺点:不能修改

实现

递推

\(p(i,j)=\max(p(i,j-1),p(i+2^{j-1},j-1))\)

查询

令 k = r-l+1;
int t = log2(k);
ans = max(p[l][t], p[r-(1<<t)+1][t]);

复杂度

\(O(logn+q)\)

代码

$\color{green}\text{点击查看代码}$
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#pragma GCC optimize (3) // O(3)
using namespace std;

int p[100010][20], lg[100010];

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while (ch>='0'&&ch<='9') { x=x*10+ch-48; ch=getchar(); }
	return x*f;
}

int main()
{
	ios::sync_with_stdio(false);
	int n, m;
	n=read();m=read();
	for(int i = 1;i <= n;i++) p[i][0]=read();
	for(int j = 1;(1<<j) <= n;j++)
		for(int i = 1;i+(1<<j-1) <= n;i++)
			p[i][j] = max(p[i][j-1], p[i+(1<<j-1)][j-1]);
	lg[1] = 0;
	for(int i = 2;i <= n;i++) lg[i] = lg[i>>1] + 1;
	while(m--)
	{
		int l, r;
		l=read();r=read();
		int t = lg[r-l+1];
		printf("%d\n",max(p[l][t], p[r-(1<<t)+1][t]));
	}
	return 0;
}

技巧-快速读入

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while (ch>='0'&&ch<='9') { x=x*10+ch-48; ch=getchar(); }
	return x*f;
}

背下来就好了

标签:ch,int,复杂度,树论,ST,while,RMQ,getchar
From: https://www.cnblogs.com/ghivan911/p/17654080.html

相关文章

  • 【STM32】4_0 基础定时器
    基础定时器TIME6和TIME7基本定时器•16位计数器(Counter):基础定时器内部有一个16位的自动增减计数器。计数器可以通过软件或外部触发递增。•时钟源(ClockSource):基础定时器可以使用不同的时钟源作为计数器的输入时钟。通常,它可以选择使用内部时钟(如系统时钟)或外部时钟(如外部......
  • cona install 出现SSLError
    Solvingenvironment:failedCondaHTTPError:HTTP000CONNECTIONFAILEDforurl<https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/noarch/repodata.json>Elapsed:-AnHTTPerroroccurredwhentryingtoretrievethisURL.HTTPerrorsareofteninte......
  • Nginx内置lua版OpenResty拦截转发请求Redis等操作
    Nginx内置lua版OpenResty拦截转发请求Redis等操作1下载并安装OpenRestyhttp://openresty.org/cn/download.html2下载lua-resty-http-0.17.1库以让openresty的lua支持外部http访问能力lua-resty-http-0.17.11下载lua-resty-http-0.17.12然后将文件中lua-resty-http......
  • 【STM32】4_0 基础定时器
    基础定时器TIME6和TIME7基本定时器•16位计数器(Counter):基础定时器内部有一个16位的自动增减计数器。计数器可以通过软件或外部触发递增。•时钟源(ClockSource):基础定时器可以使用不同的时钟源作为计数器的输入时钟。通常,它可以选择使用内部时钟(如系统时钟)或外部时钟(如外部......
  • Docker 安装 OpenResty教程
    Docker部署1.拉取镜像PSC:\Users\Administrator>dockerpullopenresty/openresty2.启动openrestyPSC:\Users\Administrator>dockerrun-d--nameopenresty-p9000:80openresty/openresty3.创建挂载目录PSC:\Users\Administrator>mkdir-p/docker/openre......
  • StableVideo:使用Stable Diffusion生成连续无闪烁的视频
    使用StableDiffusion生成视频一直是人们的研究目标,但是我们遇到的最大问题是视频帧和帧之间的闪烁,但是最新的论文则着力解决这个问题。本文总结了Chai等人的论文《StableVideo:Text-drivenconsistency-awareDiffusionVideoEditing》,该论文提出了一种新的方法,使扩散模型能......
  • 瑞萨 --- error pe0.const overlaps .text
    1.更改代码编译优化等级。2.更新section配置。 ......
  • 【STM32】3_0 中断
    中断和事件在STM32微控制器中,中断和事件是用于处理外部事件和内部状态改变的重要机制。它们允许微控制器在特定条件下停止当前执行的任务,转而处理更为紧急或重要的任务。以下是关于STM32中断和事件的一些基本信息:中断(Interrupts):中断是在微控制器执行某个任务时,突然发生的外部......
  • ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘
    1251clientdoesnotsupportauthenticationprotocolrequestedbyserver;considerupgradingMysqlclientERROR1396(HY000):OperationALTERUSERfailedfor'root'@'localhost'先登录mysqlmysql-uroot-p输入密码mysql>usemysql;mysql>......
  • RatingEstimator
    [ABC292Ex]RatingEstimator题意可以转换为:单点修改。查询如果不存在某一个前缀平均值不低于\(B\),则输出整体平均值;否则,输出第一个不低于\(B\)的前缀平均值。看到题,感觉可以线段树,但是要找的是第一个不低于的前缀平均值,怎么搞呢?首先我们可以维护前缀和,这样操作1......