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 ...
随机推荐
- 20170802,css样式优先级
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- 笨办法学Python(十)
习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...
- ring0 暴力枚举进程
原理:遍历进程ID,然后openprocess,能打开的都枚举出来 ring0 : #include "EnumProcessByForce.h" extern char* PsG ...
- MySQL入门很简单: 6 视图
1. 视图含义作用 视图是虚拟的表,是从数据率中一个或多个表中导出来的表: 数据库中只存放了视图的定义,没有存放视图中的数据,数据在原先的表中: 一旦表中的数据发生变化,显示在视图中的数据也会发生 ...
- redux详解
redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...
- numpy 矩阵运算
8.2 矩阵(Matrix)对象 Matrix类型继承于ndarray类型,因此含有ndarray的所有数据属性和方法.Matrix类型与ndarray类型有六个重要的不同点,当你当Matrix对象当 ...
- 第16章 STM32中断应用概览—零死角玩转STM32-F429系列
第16章 STM32中断应用概览 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...
- c/c++基础
如果有你认为重要的知识点,而我这却没有记录下来的,那么期待你分享给我(^U^)ノ~YO. 1.在结构体中,符号->的前面是指针变量,符号.的前面是普通变量. 程序中a->b等价于(*a ...
- scoped,会使设置UI组件库的样式识别不出来
未设置 scoped 作用域:显示效果 设置作用域的效果:ui组件默认的值(你怎么设置都不管用)
- floyed dij spfa 模板
/* SPFA模板 */ const int inf=0x3f3f3f3f; inline int SPFA(int s){ memset(dis,inf,sizeof(dis)); queue< ...