POJ3246
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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
#include<algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
int a[];
int dp[][];//2^16长度
int DP[][];
int n,q;
void ST()
{
for (int i = ; i <=n ; ++i) {
dp[i][]=a[i];
DP[i][]=a[i];
} for (int j = ; (<<j) <=n ; ++j) {
for (int i = ; i+(<<j)- <= n ; ++i) {
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);//需要注意+的优先级高于<<
DP[i][j]=max(DP[i][j-],DP[i+(<<(j-))][j-]);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for (int i = ; i <=n ; ++i) {
scanf("%d",&a[i]);
}
ST();
int x,y;
for (int i = ; i <q ; ++i) {
scanf("%d%d",&x,&y);
int m=floor(log((double)(y-x+))/log(2.0));
int MAX=max(DP[x][m],DP[y-(<<m)+][m]);
int MIN=min(dp[x][m],dp[y-(<<m)+][m]);
printf("%d\n",MAX-MIN);
}
return ;
}
POJ3246的更多相关文章
- POJ3368(RMQ)
Frequent values Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreas ...
随机推荐
- [vijos]lxhgww的奇思妙想(长链剖分)
题意 题目链接 Sol 长链剖分 又是一个用各种花式技巧优化的暴力 它的主要思想是:对于每个节点,把深度最深的子节点当做重儿子,它们之间的边当做重边 这样就会有一些非常好的轻质 所有链长总和是\(O( ...
- C++ Knowledge series Inheritance & RTTI & Exception Handling
Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...
- 绘图之Canvas学习
一 Canvas的用法 博客:http://blog.taorenjia.com/?p=237 1.drawCircle(float cx, float cy, float radius, ...
- IEnumerable<T> 用法
//以下参考来自 http://www.cnblogs.com/wilber2013/p/4299529.html
- Cocos2D-HTML5 Android项目编译
首先要有 Eclipse 和 ADT.CDT.Android SDK.Android NDK.新建一个文件MoonWar_Apk.cmd,将以下代码拷贝其中,双击之即可. set EngineHome ...
- selenium 移动端测试
selenium 可以进行移动测试的.Anroid ,IOS .进行移动端测试,需要相应的SDK ,apk.其中anriod 是selenium 支持有2中选择,remote server 和 tes ...
- Lucene学习入门——下载初识
本文从官网下载Lucene开始,一步一步进行Lucene的应用学习研究.下载初识Snowball Stemmer 1.下载 (1)首先,去Lucne的Apache官网主页 http://lucene. ...
- dd-wrt ddns更新失败由于电信提供的ip不是公网ip
由于电信提供的ip地址原来是公网的ip,后来电信通过nat提供一个内网ip,导致ddns更新失败.电话给电信客服10000号,让他们修改回来之后就可以了. 如果ddns更新失败,尤其是原本是正常的,后 ...
- soap使用xml调用webapi后返回xml信息进行JSON转换处理,以顺丰查询接口为例
expressUrl = string.Format(可以卸载配置文件的域名URL + "/bsp-oisp/ws/expressService"); StringBuilder ...
- 最长上升子序列&&最长不下降子序列
百练2757: 题目描述: 对于给定的序列,求出最长上升子序列的长度. 题目链接:http://bailian.openjudge.cn/practice/2757 解题思路 一.动态规划 1. 找子 ...