st表(poj3264)
st表很像线段树,但线段树既能查询和修改,而st表只能查询。
首先我们先用二维数组建立一个表,st[i][j]表内存的是从第i位开始1<<j范围内的best(st[i][j-1],st[i+(1<<j-1)][j-1])
由上面的公式可知st[i][j]是由st[i][j-1],st[i+(1<<j-1)][j-1]更新而来的,我们可以将st[i][j]分成两段,由1<<j=2*(1<<j-1)可得一段是st[i][j-1],另一段则是st[i+(1<<j-1)][j-1]。
然后再用log以2为底n的对数对输入的长度做处理,其中可以用if(1 << Log2[i]+1 == i) Log2[i] ++做判断。
例题:poj3264
description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
input
Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
output
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
sample input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
sample output
6
3
0
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 50010
int stmax[maxn][],stmin[maxn][],Log2[maxn];
int n,q;
void init()
{
for(int j = ; (<<j) <= n; j ++)
for(int i = ; i +(<<j)- <= n ; i ++){
stmax[i][j] = max(stmax[i][j-],stmax[i+(<<j-)][j-]);
stmin[i][j] = min(stmin[i][j-],stmin[i+(<<j-)][j-]);
}
Log2[] = ;
for(int i = ; i <= n; i ++)
{
Log2[i] = Log2[i-];
if( << Log2[i]+ == i) Log2[i] ++;
}
} int query(int l,int r)
{
int len = r - l + ;
int k = Log2[len];
int maxx = max( stmax[l][k],stmax[r-(<<k)+][k]);
int minn = min( stmin[l][k],stmin[r-(<<k)+][k]);
return maxx - minn;
} int main()
{
int l,r;
scanf("%d%d",&n,&q);
for(int i = ; i <= n; i ++)
{
scanf("%d",&stmax[i][]);
stmin[i][] = stmax[i][];
}
init();
while(q--){
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r));
}
}
st表(poj3264)的更多相关文章
- ST表poj3264
/* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...
- ST表入门学习poj3264 hdu5443 hdu5289 codeforces round #361 div2D
ST算法介绍:[转自http://blog.csdn.net/insistgogo/article/details/9929103] 作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 方 ...
- poj3264 倍增法(ST表)裸题
打出st表的步骤:1:建立初始状态,2:区间按2的幂从小到大求出值 3:查询时按块查找即可 #include<iostream> #include<cstring> #incl ...
- POJ3264:Balanced Lineup——题解+st表解释
我早期在csdn的博客之一,正好复习st表就拿过来.http://write.blog.csdn.net/mdeditor#!postId=63713810 这道题其实本身不难(前提是你得掌握线段树或 ...
- [poj3264]rmq算法学习(ST表)
解题关键:rmq模板题,可以用st表,亦可用线段树等数据结构 log10和log2都可,这里用到了对数的换底公式 类似于区间dp,用到了倍增的思想 $F[i][j] = \min (F[i][j - ...
- 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] ...
随机推荐
- 20175314 《Java程序设计》第八周学习总结
20175314 <Java程序设计>第八周学习总结 教材学习内容总结 安利一个非常实用的图片处理工具:图片工厂,它具有非常强大的图片批处理能力,比如加水印.降低画质.命名等,不仅如此它还 ...
- Python学习笔记十_模块、第三方模块安装、模块导入
一.模块.包 1.模块 模块实质上就是一个python文件.它是用来组织代码的,意思就是把python代码写到里面,文件名就是模块的名称,test.py test就是模块的名称 2.包 包,packa ...
- 【算法】BM算法
目录 BM算法 一. 字符串比较的分析 二.BM算法的思想 三.算法实现 BM算法 @ 一. 字符串比较的分析 如果要判定长度为\(n\)两个字符串相等,比较中要进行\(n\)比较,但是如果要判定两个 ...
- java_29打印流
1打印流 PrintStream 和PrintWriter 不负责数据源 只负责数据目的 2.方法 public class Demo { public static void main(St ...
- CentOS7 安装VNC
系统环境:CentOS Linux release 7.6.1810Kernel:3.10.0-957.el7.x86_64系统现状:最小化安装,没有安装任何图形支持软件 安装图形化支持 不建议安装G ...
- Asp.net core中实现自动更新的Option
Asp.net core可以监视json.xml等配置文件的变化, 自动刷新内存中的配置内容, 但如果想每隔1秒从zookeeper.consul获取最新的配置信息, 需要自己实现. 阅读了 Asp. ...
- 查看linux是否为虚拟机,以及其它信息,cpu,主机型号,主板型号等
dmidecode -s system-product-name 物理机: [root@swnode1]# dmidecode -s system-product-name I840-GS 虚拟机: ...
- node,Yeoman,Bower,Grunt的简介及安装
作为前端,基本的html,css,js已经不太够用了,所以要学习一些前端自动化工具,来提高我们的生产力 1.NodeJS 先安装NodeJS,直接去官网,下载最新的版本,一定要最新的版本,这样会避免很 ...
- spring事务相关
在 SPRING 中一共定义了六种事务传播属性 PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPOR ...
- 安装jdk1.8导致eclipse显示问题
安装jdk1.8后新建workspace(mars)后eclipse的toolbar和主题显示有问题 例如: 修改步骤 1.设置主题window->Preferences->General ...