RMQ (st表) Balanced Lineup
来源:互联网 发布:如何防范网络诈骗ppt 编辑:程序博客网 时间:2024/06/05 09:36
RMQ (st表)
Balanced Lineup
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<stdio.h>#include<algorithm>#include<math.h>using namespace std;long long i,j,k,l,dpmax[50055][20],dpmin[50055][20],a[50055],r,n,m,max1,min1;//dpmax[i][j]代表以i位开始的后面j位的最大值//(1<<k)代表2^kint main(){ scanf("%lld%lld",&n,&m); for(i=1;i<=n;i++) { scanf("%lld",&a[i]); dpmax[i][0]=a[i]; dpmin[i][0]=a[i]; } for(j=1;j<=16;j++) { for(i=1;i<=n;i++) { if(i+(1<<j)-1<=n) { dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]); dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]); } } } while(m--) { scanf("%lld%lld",&l,&r); k=log2(r-l+1); max1=max(dpmax[l][k],dpmax[r-(1<<k)+1][k]); min1=min(dpmin[l][k],dpmin[r-(1<<k)+1][k]); printf("%lld\n",max1-min1); } return 0;}
- RMQ (st表) Balanced Lineup
- poj3264 Balanced Lineup(RMQ +st)
- POJ3264 Balanced Lineup (RMQ & ST)
- Balanced Lineup-POJ - 3264-RMQ线段树/st表
- Poj 3264 Balanced Lineup【RMQ----ST算法】
- [RMQ之稀疏表(ST表)讲解及例题][POJ 3264]Balanced Lineup
- 【bzoj 1699 & 1636】【POJ 3264】Balanced Lineup(st表|RMQ)
- RMQ的ST算法学习小记 Poj 3264 Balanced Lineup
- [POJ 3264]Balanced Lineup(ST算法求RMQ)
- POJ3264 Balanced Lineup 线段树 RMQ ST算法应用
- POJ 3264 Balanced Lineup RMQ问题 ST算法
- 【POJ】3264 - Balanced Lineup(RMQ - ST算法 || 线段树)
- (模板题)poj 3264 Balanced Lineup(RMQ的ST算法)
- POJ 3264-Balanced Lineup(RMQ-ST算法)
- 【POJ】【P3264】【Balanced Lineup】【题解】【ST表】
- POJ 3264 Balanced Lineup ST表
- poj 3264 Balanced Lineup ST表
- POJ3264 Balanced Lineup 线段树|ST表
- [源码学习][知了开发]WebMagic四大组件-Scheduler
- STL set容器 基本运用
- 不可思议、违反直觉
- (三)Spring框架——IoC容器
- TCP UDP及socket编程学习(一)
- RMQ (st表) Balanced Lineup
- (四)Spring框架——Bean的定义
- 会话技术 cookie和session 学习笔记
- Spark分区器HashPartitioner和RangePartitioner代码详解
- 动画经验总结
- JAVA面向对象-----局部内部类
- 物体检测-回归方法(YOLO+SSD)
- 使用System.arraycopy()实现数组之间的复制
- LeetCode-268:Missing Number