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. Vue编译时写在style中的路径问题

    写在vue文件里面的style样式,在添加例如背景图片的时候,如果用的是相对路径,那么build出来的css文件的路径将会出错,导致找不到图片. 通过查找资料,在https://segmentfaul ...

  2. mint-ui popup自动关闭

    <template> <div class="hello"> <input type="text" v-model="n ...

  3. C++ Knowledge series Layering

    Programming has its own methodology. Layering is everywhere in real life,this why the pruchase and s ...

  4. 通过adb获取应用的Activity堆栈信息

    获取所用应用 adb shell dumpsys activity 获取自己的应用 adb shell dumpsys activity | grep 应用的package 获取处于栈顶的activi ...

  5. 新版mysql 5.7的group_by非常不和谐

    sqlalchemy.exc.OperationalError OperationalError: (_mysql_exceptions.OperationalError) (1055, " ...

  6. System Center Configuration Manager 2016 配置安装篇(Part1)

    SCCM 2016 配置管理系列(Part 1- 4) 介绍AD01上配置了Active Directory域服务(ADDS),然后将Configuration Manager服务器(CM16)加入到 ...

  7. 【转】C内存操作函数

    一.malloc/calloc 名称: Malloc/calloc 功能:  动态内存分配函数 头文件: #include <stdlib.h> 函数原形: void *malloc(si ...

  8. C++11 新特性之 序列for循环

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/lr982330245/article/details/30971195 在C++中在C++中for循 ...

  9. 【[ZJOI2015]诸神眷顾的幻想乡】

    题目 听说这是广义\(SAM\)的板子 看来对于广义\(SAM\)我也就只会板子了 叶子数很少,所以可以枚举每一个叶子节点作为根建一遍\(Trie\)树 只需要对\(Trie\)树建出\(SAM\)就 ...

  10. 统计函数运行时间-CPU端

    C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:  clock_t clock( void ); 这个函数返回从“开启这个程序 ...