Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 52328   Accepted: 24551
Case Time Limit: 2000MS

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<iostream>
#include<cstdio>
#include<cstring> #define maxn 1000000 using namespace std;
int n,m,ans,x,y,a[maxn],p[maxn];
int f1[maxn][],f2[maxn][]; inline int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;} void ST()
{
for(int i=;i<=n;i++)
f1[i][]=f2[i][]=a[i];
for(int j=;j<=;j++)
for(int i=;i+(<<j)-<=n;i++)
{
f1[i][j]=min(f1[i][j-],f1[i+(<<j-)][j-]);
f2[i][j]=max(f2[i][j-],f2[i+(<<j-)][j-]);
}
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
if((<<j)>i)
{
p[i]=j-;
break;
}
} int query(int l,int r)
{
int k=p[r-l+];
int ans1=min(f1[l][k],f1[r-(<<k)+][k]);
int ans2=max(f2[l][k],f2[r-(<<k)+][k]);
return ans2-ans1;
} int main()
{
n=init();m=init();
for(int i=;i<=n;i++)
a[i]=init();
ST();
for(int i=;i<=m;i++)
{
x=init();y=init();
printf("%d\n",query(x,y));
}
return ;
}

最新文章

  1. Block 代码快
  2. Leetcode-190 Reverse Bits
  3. 初识JavaScript 变量, 操作符, 数组
  4. Vigen&#232;re密码
  5. php操作mongodb中的ISODate格式日期
  6. Face++ – 提供给你实时的脸部识别 API
  7. web项目的日志打印位置设置
  8. Android动画效果生动有趣的通知NiftyNotification(Android Toast替代品)
  9. hdu 3790 最短路径问题(最短路,Dijsktra)
  10. 迷途指针 new delete
  11. Quartz Scheduler 开发指南(1)
  12. (转) Spring读书笔记-----Spring的Bean之配置依赖
  13. 双向链表JAVA代码
  14. sql优化-总结
  15. Linux通过使用Sambaserver示例
  16. ESFramework 4.0 快速上手(01) -- Rapid引擎
  17. 【BZOJ 1002】: [FJOI2007]轮状病毒
  18. python3操作MySQL数据库,一次插入多条记录的方法
  19. Python学习计划
  20. &#39;Tensorboard.util&#39; has no attribute &#39;Retrier&#39; - &#39;Tensorboard.util&#39;没有属性&#39;Retrier&#39;

热门文章

  1. React和Jquery比较
  2. java学习日志--char和int的相互转换
  3. NOIP 前的垂死挣扎
  4. 微信小程序官方指南手册,教你如何使用微信小程序!
  5. 洛谷 1569 [USACO11FEB]属牛的抗议
  6. code wars quiz: toInteger
  7. BZOJ(2) 1041: [HAOI2008]圆上的整点
  8. V Server Ubuntu
  9. jeewx 微信管家 - 举办商业版本号免费试用活动
  10. 【JAVA】两点经纬度直线距离的计算