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<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的更多相关文章

  1. POJ3368(RMQ)

    Frequent values Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreas ...

随机推荐

  1. cf633F. The Chocolate Spree(树形dp)

    题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形dp的 也就是有几种情况,可以讨论一下.. 下文的"最大值"指的是& ...

  2. 2602 最短路径问题Dihstra算法

    题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线.若有连线,则表示可从一个点到达另一个点,即两点间有通路,通 ...

  3. sass入门(一)

    一].sass入门安装sass安装koala // sass中可以自定义变量 $fontStack: Microsoft Yahei; $primaryColor: #333; body { font ...

  4. Linux文件属性与权限

    一.在Linux里面,任何一个文件都具有“User,Group,Others”(用户.用户组.其他人)三种身份 二.用户组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个用户组的 ...

  5. ES6相关特性(解构赋值)

    解构赋值:本质上是一种匹配模式,等号两边的模式相同,则左边的变量可以被赋予对应的值. 注意:null & undefined 不能解构赋值!!! 数组的解构赋值: let [a,[[b],c] ...

  6. nginx配置vhost配置文件详解

    //千锋PHP-PHP培训的实力派server { listen 80; server_name www.sina.com; root /data/www/sina; index index.php; ...

  7. PDO数据库类——对query()和exec()的异常监听

    PDO异常类中,query()和exec()方法中执行失败时,默认情况下,我们是无法知道,具体执行失败的原因. 那如果我们想要监听异常的话,肿么整呢? 只要使用setAttribute()方法,即可监 ...

  8. traffic_light_bag_file 数据集 下载链接

    链接:https://pan.baidu.com/s/19p5aGRfs6iFtN_SWAxCkRQ 密码:v9wx

  9. IOS NSOperationQueue(线程 封装操作)

    #import "HMViewController.h" @interface HMViewController () @end @implementation HMViewCon ...

  10. P2082 区间覆盖(加强版)

    题目 #include<iostream> #include<algorithm> #include<cstring> using namespace std; s ...