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.. NQ+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 题意,给出一串数字,有m个询问,询问l-r之间最大值和最小值的差. 好吧,这明显可以用线段树做....我知道....但可以离线实在让我忍不住了....不管了....st做法奉上.... 于是,收到了素质RE四连...

是是是,我以为网卡了,然后手贱直接交了4发,仔细一查原来j没算对,开大了....

然后改一发A掉了

至于长度emmmm这只是暴力压行的结果,无视就行了.....

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int dp1[][],dp2[][],a[]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dp1[i][]=a[i];
dp2[i][]=a[i];
}
for(int j=;j<=;j++)
{
for(int i=;i<=n;i++)
{
if(i+(<<j)-<=n)
dp1[i][j]=max(dp1[i][j-],dp1[i+(<<(j-))][j-]);
dp2[i][j]=min(dp2[i][j-],dp2[i+(<<(j-))][j-]);
}
}
for(int i=;i<=m;i++)
{
int l,r,ans=;
scanf("%d%d",&l,&r);
int x=(int)(log((double)(r-l+))/log(2.0));
int max1=max(dp1[l][x],dp1[r-(<<x)+][x]);
int min1=min(dp2[l][x],dp2[r-(<<x)+][x]);
ans=max1-min1;
printf("%d\n",ans);
}
}

果然ST看着比线段树短多了....

每天刷题,身体棒棒!

												

最新文章

  1. HTML(六)——表单验证、正则表达式、事件
  2. 网络之Ip地址
  3. 【linux】find删除指定时间之前的文件
  4. poolboy的坑
  5. linux eclipse cdt make error 127
  6. BroadcastReceiver的实例----基于Service的音乐播放器之一
  7. Java基础知识强化之多线程笔记06:Lock接口 (区别于Synchronized块)
  8. JDBC学习总结(三)
  9. fgetc, getchar(), fscanf的问题
  10. Ubuntu上glibc CVE-2015-7547漏洞的POC验证和修复
  11. jQuery Builder
  12. chrome开发工具指南(六)
  13. 2017年最受欢迎的UI框架
  14. 如何在Cocos2D 1.0 中掩饰一个精灵(四)
  15. JS全角与半角转化小结
  16. 无法解析db.properties,spring报错:Caused by: java.sql.SQLException: unkow jdbc driver : ${url}
  17. 零基础学习hadoop到上手工作线路指导(中级篇)
  18. zookeeper客户端相关命令
  19. WordPress 后台添加额外选项字段到常规设置页面
  20. 微信小程序 - 自定义components组件详解A篇

热门文章

  1. mysql、mariadb安装和多实例配置
  2. C#单例测试(懒汉式双锁保证线程安全)
  3. Hive基础(3)---Fetch Task(转)
  4. 21.Linux-写USB键盘驱动(详解)
  5. Python自学笔记-time模块(转)
  6. jdbc-批处理
  7. 关于如何绑定Jquery 的scroll事件(兼容浏览器 Wookmark瀑布流插件)
  8. WPF ListBox数据绑定
  9. DevOps之归纳总结
  10. plsql中文乱码问题方案解决