首页 > 其他分享 >倍增之ST表

倍增之ST表

时间:2024-07-27 19:50:17浏览次数:7  
标签:lg ch int st ST 倍增 dp

ST表是什么

  • ST表其实不叫ST表,因为ST中的T已经是table表的意思了
  • ST表适合用来处理RMQ,LCA等经典问题
  • st[i][j] 代表 区间[i,i+2^j-1]
  • st[i][0]=a[i] (初始化)

模板题1

每天,你们学校有N(1 <= N <= 50,000)个人按同一序列排队做操. 有一天, 老师让你找一队连续序列的人来进行比赛. 但是为了避免水平悬殊,他们的不应该相差太大. 你准备从Q (1 <= Q <= 200,000) 个可能的选择中选拔,给你所有人的身高 (1 <= 身高 <= 1,000,000). 你需要计算出每一组里面最高和最矮的人的身高差别.

  • st表,开两个数组(st和dp),一个记录个子高的,一个记录个子矮的
  • 用两个函数初始化st,dp
  • st[i][0]=dp[i][0]=a[i] (初始化)
  • c++函数log2():对后面的Q次询问有用
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
int st[N][20], n, q, x, y, lg;
int dp[N][20];
void stm() {
    for (int j = 1; (1 << j) <= n; j++) 
	for (int i = 1; i + (1 << j) - 1 <= n; i++) 
		st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
void stn() {
    for (int j = 1; (1 << j) <= n; j++) 
	for (int i = 1; i + (1 << j) - 1 <= n; i++) 
		dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
int main() {
    cin >> n>>q;
    for (int i = 1; i <= n; i++) cin >> st[i][0];
    for (int i = 1; i <= n; i++) dp[i][0]=st[i][0];
    stm(),stn();
    while (q--){
	cin >> x >> y;
	lg = (int)log2(y - x + 1),;
	cout << max(st[x][lg], st[y - (1 << lg) + 1][lg]) - min(dp[x][lg], dp[y - (1 << lg) + 1][lg]) << endl;
    }
    return 0;
}

模板题2

  • 是原题网址
  • 跟上一题差不多,max/min只留一个max就行了(st和dp只留st)
  • 不加快读WA 3个,不加快写也WA 3个,两个都不加WA 6个
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
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;
}
inline void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
int st[N][20], n, q, x, y, lg;
void stm() {
    for (int j = 1; (1 << j) <= n; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
            st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    n = read(), q = read();
    for (int i = 1; i <= n; i++) st[i][0] = read();
    stm();
    while (q--) {
        x = read(), y = read();
        lg = (int)log2(y - x + 1);
        write(max(st[x][lg], st[y - (1 << lg) + 1][lg]));
        putchar('\n');
    }
    return 0;
}

模板题3(RMQ之gcd)

  • 把模板题2的max换成__gcd(辗转相除也行)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int st[N][20], n, q, x, y, lg;
void stm() {
    for (int j = 1; (1 << j) <= n; j++)
	for (int i = 1; i + (1 << j) - 1 <= n; i++) 
		st[i][j] = __gcd(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int main() {
    cin >> n>>q;
    for (int i = 1; i <= n; i++)
	cin >> st[i][0];
    stm();
    while (q--){
	cin >> x >> y,;
	lg = (int)log2(y - x + 1);
	cout << __gcd(st[x][lg], st[y - (1 << lg) + 1][lg]) << endl;
    }
    return 0;
}

本人比较菜,如有不足,欢迎各位dalao前来指正!

标签:lg,ch,int,st,ST,倍增,dp
From: https://www.cnblogs.com/algorithm-hu/p/18327363

相关文章

  • 实验说明 - ssti
    实验名称简单的ssti实验简介是一种针对服务器端模板的注入漏洞。实验说明攻击者将恶意代码输入到模板服务器在执行时未对恶意代码进行处理就输出执行将字符串当作模板执行ssti注入就是使其渲染我们想要执行的的字符串实验步骤步骤一:输入{{7*7}}判断类型步骤二:{{......
  • SMU Summer 2024 Contest Round 8
    SMUSummer2024ContestRound8Product思路注意到\(\prod\limits_{i=1}^NL_i\le10^5\),也就是说N不会超过16,因为\(2^{17}>10^5\),所以我们可以直接暴搜。代码#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;intmain(){ios::sync_with......
  • llama-agentic-system
    文章目录一、关于llama-agentic-system二、LLama代理系统安装和设置指南1、创建Conda环境2、运行FP83、作为包安装4、测试安装5、下载检查点(或使用现有模型)6、配置推理服务器配置7、运行推理服务器8、配置代理系统9、为工具添加API密钥10、启动应用程序并与服务器交互11......
  • fastjson反序列化漏洞原理及<=1.2.24&<=1.2.47&Fastjson v1.2.80简单利用&不出网判断&修
    1、什么是fastjsonfastjson是一个有阿里开发的一个开源Java类库,可以将Java对象转换为JSON格式(序列化),当然它也可以将JSON字符串转换为Java对象(反序列化)。2、漏洞原理FastJson在解析json的过程中,⽀持使⽤autoType来实例化某⼀个具体的类,并调⽤该类的set/get⽅法......
  • Visual Studio版本号、MSVC版本、工具集版本号
    IDE              发布时间   工具集版本   MSC_VER      MSVC++          系统支持       使用频率VisualC++6.0       1998       V60       1200......
  • 【0299】Postgres内核之 INSERT INTO 原始解析树 转 Query 树 (3)
    相关文章:【0297】Postgres内核之INSERTINTO原始解析树转Query树(1)【0298】Postgres内核之INSERTINTO原始解析树转Query树(2)1.opentable(RangeVar指定)在完成了由函数setup_parser_errposition_callback()完成的解析器错误位置报告回调函数的注册后,接下来通......
  • Windows系统更新R版本及Rstudio
    由于一些包对R的版本的要求比较高,所以有时候我们不得不更新R的版本。但是呢,更新了R版本后,另外有些包的版本又不兼容,唉,更新R包的版本又很费时,所以一般能不更新就不更新吧。下面介绍一下常见的更新R的方法吧。一、更新R版本(1)在RGui或Rstudio中使用以下代码(推荐RGui) #install......
  • 文件编码检测-Python解决UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x
    #检测数据编码格式importchardetwithopen('附件1.csv','rb')asf:result=chardet.detect(f.read())#读取一定量的数据进行编码检测print(result['encoding'])#打印检测到的编码在读取文件时会遇到各种问题,UnicodeDecodeError:'utf-8'codeccan'tde......
  • ISTQB初级
    缺陷的根因分析,从而促进缺陷预防;缺陷的根因分析,从而不断的对测试流程进行改进;     ......
  • ast获取指定python文件中携带指定装饰器函数的实现
    在实现自动化测试过程中,需要根据指定的装饰器来标记需要执行的用例或函数,下面根据使用ast库来实现读取指定文件中的数据结构后对其内容进行解析并拿到携带对应装饰器的函数。根据以下方法仅能解析func、class-func的数据结构,其余数据结构可能不兼容,需要根据实际情况进行完善调整......