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)的更多相关文章

  1. ST表poj3264

      /* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...

  2. ST表入门学习poj3264 hdu5443 hdu5289 codeforces round #361 div2D

    ST算法介绍:[转自http://blog.csdn.net/insistgogo/article/details/9929103] 作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 方 ...

  3. poj3264 倍增法(ST表)裸题

    打出st表的步骤:1:建立初始状态,2:区间按2的幂从小到大求出值 3:查询时按块查找即可 #include<iostream> #include<cstring> #incl ...

  4. POJ3264:Balanced Lineup——题解+st表解释

    我早期在csdn的博客之一,正好复习st表就拿过来.http://write.blog.csdn.net/mdeditor#!postId=63713810 这道题其实本身不难(前提是你得掌握线段树或 ...

  5. [poj3264]rmq算法学习(ST表)

    解题关键:rmq模板题,可以用st表,亦可用线段树等数据结构 log10和log2都可,这里用到了对数的换底公式 类似于区间dp,用到了倍增的思想 $F[i][j] = \min (F[i][j - ...

  6. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  7. 【BZOJ-2006】超级钢琴 ST表 + 堆 (一类经典问题)

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2473  Solved: 1211[Submit][Statu ...

  8. 【BZOJ-3956】Count ST表 + 单调栈

    3956: Count Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 99[Submit][Status][Discuss] ...

  9. 【BZOJ-4569】萌萌哒 ST表 + 并查集

    4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 459  Solved: 209[Submit][Status] ...

随机推荐

  1. layabox typescript 安装固定版本

    安装最新版本方法: https://blog.csdn.net/adelais__/article/details/79181474 固定版本(比如2.1.5): C:\Users\Administr ...

  2. 关于easyui框架中a标签使用onclick()触发事件偶尔会选项卡消失BUG解决方案

    今天发现公司的一个easyui项目中有个页面会在触发onclick事件时选项卡消失,如下图 产生BUG后 产生BUG前 查找很多地方还有资料不知道哪里出现的问题,看了下框架源码之类的,因为不是专门的前 ...

  3. oracle 中查看数据库表中某个字段是否重复

    1.select  表中重复的字段 from  表名 group by 表中的重复的字段 HAVING count(表中的重复的字段)>1 举例说明 : 表名 : psp_cell_model  ...

  4. labellmg的使用

    ---恢复内容开始--- 在powershell环境下进入到labelImg解压后的文件,我这里是D:\labelImg-master\labelImg-master 执行命令: pyrcc5 -o ...

  5. 1. NES简介

    NES(Nintendo Entertainment System)简介 NES是北美地区对任天堂发行的第三代家用游戏机的简称. 1.CPU NES使用一颗理光[1]制造的8位2A03 NMOS处理器 ...

  6. FastDFS数据存储

    1. 数据存储 在fdfs传一份文件时,通常会返回下面的一串字符,这包含了该文件在服务器端一些存储信息 M00/00/00/wKg4C1tFmTWAFPKBAADdeFFxlXA240.png 下面解 ...

  7. es5的语法学习

    1. strict模式 严格模式,限制一些用法,'use strict'; 2. Array增加方法 增加了every.some .forEach.filter .indexOf.lastIndexO ...

  8. 算法 BF算法

    BF算法是字符匹配的一种算法,也称暴力匹配算法 算法思想: 从主串s1的pos位置出发,与子串s2第一位进行匹配 若相等,接着匹配后一位字符 若不相等,则返回到s1前一次匹配位置的后一位,接着与s2的 ...

  9. c语言相关概念

    2019-04-06 a文件 库是预编译的目标文件(object files)的集合,它们可被链接进程序.静态库以后缀为‘.a’的特殊的存档文件(archive file)存储. a文件转so文件:h ...

  10. 检测2个url的不同之处(爬虫分析接口)

    就是简单的检测2个url的不同之处,在做爬虫时,要分析接口地址的不同之处,靠自己的眼睛有点累,所以写了一个小程序,不喜勿喷 #测试数据 a = "https://list.tmall.com ...