[RMQ之稀疏表(ST表)讲解及例题][POJ 3264]Balanced Lineup
来源:互联网 发布:vb编写简单整人小程序 编辑:程序博客网 时间:2024/05/16 08:58
讲解:
介绍:
稀疏表(Sparse Table, ST 表)与线段树、树状数组一样,也是常用来处理序列上的区间询问问题的。但 ST 表只能处理区间最值,即RMQ(Range Minimum Query)问题,它的空间需求也比前两者要大,是
算法流程:
1、稀疏表其实在维护一个个长度为
2、区间
而j的大小是
PS:求log有函数,但是速度较慢,用 ST表时n 的大小不会太大,最多只有
3、询问[x,y]时,可以求出
PS:此图来源于 Gao Wenyuan 的PDF。
例题:
题目描述:
题目链接:POJ 3264 Balanced Lineup
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
题目大意:
给定一个长度为 n 的序列
题目分析:
ST表模板题。
附代码:
#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<ctime>#include<cmath>#include<cctype>#include<iomanip>#include<algorithm>using namespace std;const int maxn=50010;const int logn=15;int n,m,a[maxn],minn[maxn][logn+1],maxx[maxn][logn+1];int logg[maxn],l,r,minans,maxans,ans,k;int main(){ //freopen("lx.in","r",stdin); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); logg[0]=-1; for(int i=1;i<=n;i++) { minn[i][0]=maxx[i][0]=a[i]; logg[i]=logg[i>>1]+1;//预处理1~n对应的log } for(int j=1;j<=logn;j++) for(int i=1;i+(1<<j)-1<=n;i++) { minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]); maxx[i][j]=max(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]); } for(int i=1;i<=m;i++) { scanf("%d%d",&l,&r); k=logg[r-l+1]; maxans=max(maxx[l][k],maxx[r-(1<<k)+1][k]); minans=min(minn[l][k],minn[r-(1<<k)+1][k]); ans=maxans-minans; printf("%d\n",ans); } return 0; }
- [RMQ之稀疏表(ST表)讲解及例题][POJ 3264]Balanced Lineup
- Balanced Lineup-POJ - 3264-RMQ线段树/st表
- RMQ (st表) Balanced Lineup
- [POJ 3264] Balanced Lineup (ST表)
- 【bzoj 1699 & 1636】【POJ 3264】Balanced Lineup(st表|RMQ)
- Poj 3264 Balanced Lineup【RMQ----ST算法】
- 【POJ】3264 - Balanced Lineup(RMQ - ST算法 || 线段树)
- POJ 3264-Balanced Lineup(RMQ-ST算法)
- POJ 3264 Balanced Lineup ST表
- poj 3264 Balanced Lineup ST表
- POJ 3264 Balanced Lineup(ST表)
- [学习][poj3264]稀疏表(ST表) Balanced Lineup
- RMQ的ST算法学习小记 Poj 3264 Balanced Lineup
- [POJ 3264]Balanced Lineup(ST算法求RMQ)
- POJ 3264 Balanced Lineup RMQ问题 ST算法
- (模板题)poj 3264 Balanced Lineup(RMQ的ST算法)
- POJ 3264 Balanced Lineup(ST)
- 【POJ】【P3264】【Balanced Lineup】【题解】【ST表】
- 流水作业调度(dp)
- tensorflow中的矩阵生成(数据初始化)与变换函数
- 轮播Banner使用简单集成
- 自定义View(四):onDraw()绘图详解
- js中阻止事件冒泡
- [RMQ之稀疏表(ST表)讲解及例题][POJ 3264]Balanced Lineup
- java定时调度工具quartz
- php 获取数组第一个元素 以及最后一个元素 && 最后一个元素的键名
- 55. Jump Game 【Medium】 贪心算法
- 《java高并发程序设计》读书笔记(4)
- 1111
- Codeforces Round #337 (Div. 2)-C Harmony Analysis(构造x维正交向量)
- 聊聊Android点击事件传递
- interview