POJ 3264 Balanced Lineup (RMQ)

网友投稿 880 2022-08-26

POJ 3264 Balanced Lineup (RMQ)

POJ 3264 Balanced Lineup (RMQ)

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 31734251 54 62 2

Sample Output

630

题意

给出 ​​n​​​ 个数,然后有 ​​q​​ 次查询,每次查询是一个区间,输出该区间最大值与最小值的差。

思路

​​RMQ​​​ 问题,可使用 ​​ST​​ 算法解决, O(nlogn) 的预处理, O(1)

​​dp[i][j]​​​ 代表以 ​​j​​ 为起点,区间长度为 2i

于是很容易得出状态转移方程: ​​dp[i][j]=op(dp[i-1][j],dp[i-1][j+1<<(i-1)])​​

记得初始化 ​​dp[0][j]​​ 为对应的每个数。

查询时候也很方便,找出最大的不可覆盖查询区间的区间长度,然后选取首段与尾段取最优即可。

AC 代码

#include #include#includeusing namespace std;const int maxn = 51000;const int inf = 0x3f3f3f;int maxx[20][maxn];int minn[20][maxn];int n,q;void rmq(){ for(int i=1; (1<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:jsp解决kindeditor在线编辑器struts图片上传问题
下一篇:hihocoder 1531 德国心脏病 (模拟)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~