初识 ST 表
推荐博客 : https://blog.csdn.net/BerryKanry/article/details/70177006
ST表通常用于RMQ问题中,询问某个区间的最值这类问题中
ST表的核心部分就是 st[i][j] ,表示以 i 为起点跳跃 2^j 所经路径的最值。
更新的时候利用dp的思想
代码示例 :
void init(){
LOG[0] = -1;
for(int i = 1; i <= 100000; i++) LOG[i] = LOG[i/2]+1;
for(int i = 1; i <= LOG[n]; i++){
for(int j = 1; j+(1<<i)-1 <= n; j++){
st[j][i] = max(st[j][i-1], st[j+(1<<(i-1))][i-1]);
}
}
}
至于查询是可以O(1)实现的

int ans = max(st[a][k], st[b-(1<<k)+1][k]);
还有关于求每个数的对数的LOG数组也是个重点,在上面
int st[maxn][20]; // 最大值为例
int n;
int LOG[maxn]; void init(){
LOG[0] = -1;
for(int i = 1; i <= 100000; i++) LOG[i] = LOG[i/2]+1; for(int i = 1; i <= LOG[n]; i++){
for(int j = 1; j+(1<<i)-1 <= n; j++){
st[j][i] = max(st[j][i-1], st[j+(1<<(i-1))][i-1]);
}
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); cin >> n;
for(int i = 1; i <= n; i++){
scanf("%d", &st[i][0]);
}
init();
//for(int i = 1; i <= n; i++){
//for(int j = 0; j <= LOG[n]; j++){
//printf("%d ", st[i][j]);
//}
//printf("\n");
//}
int m, a, b; // m个查询
cin >> m;
for(int i = 1; i <= m; i++){
scanf("%d%d", &a, &b); int k = LOG[b-a+1]
int ans = max(st[a][k], st[b-(1<<k)+1][k]);
printf("%d\n", ans);
}
return 0;
}
初识 ST 表的更多相关文章
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- 【BZOJ-2006】超级钢琴 ST表 + 堆 (一类经典问题)
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2473 Solved: 1211[Submit][Statu ...
- 【BZOJ-3956】Count ST表 + 单调栈
3956: Count Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 173 Solved: 99[Submit][Status][Discuss] ...
- 【BZOJ-4569】萌萌哒 ST表 + 并查集
4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 459 Solved: 209[Submit][Status] ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- HDU5726 GCD(二分 + ST表)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- Bzoj 2006: [NOI2010]超级钢琴 堆,ST表
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2222 Solved: 1082[Submit][Statu ...
- ST表poj3264
/* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...
随机推荐
- Python--day41--线程锁
1,死锁 死锁代码示例: import time from threading import Lock, Thread noodle_lock = Lock() fork_lock = Lock() ...
- java 内省综合案例和Beanutils工具包
演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...
- H3C 主动方式建立连接过程
- Linux 基础(一)stat函数
Header file: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> DEFI ...
- LightOJ - 1287 Where to Run (期望dp+记忆化)
题面: Last night you robbed a bank but couldn't escape and when you just got outside today, the police ...
- Squid使用账号密码进行认证
Squid 3.5支持ssl代理,为保证安全和滥用,可以使用简单的认证. Step1:在squid的配置文件中,添加如下: auth_param basic program /usr/lib64/sq ...
- sed & awk & grep 专题
转载自:http://www.cnblogs.com/moveofgod/p/3540575.html grep, sed 与 awk 相当有用 ! gerp 查找, sed 编辑, awk 根据内容 ...
- 从头学pytorch(三) 线性回归
关于什么是线性回归,不多做介绍了.可以参考我以前的博客https://www.cnblogs.com/sdu20112013/p/10186516.html 实现线性回归 分为以下几个部分: 生成数据 ...
- world 文档中表格旋转180°
一个好朋友给我打电话,说是有个wps操作把他难住了,他常年跟wps 形影不离,你都搞不定,我都不怎么用.听完他说的以后,我才明白他要的效果是怎么样的,贴图来看: 其实像直接转化成这种效果没有办法,但是 ...
- Google 开源的 Python 命令行库:深入 fire(一)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...