Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 42349   Accepted: 19917
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
线段树维护区间最大值、最小值,然后相减。AC代码:
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#include <set>
#include <cmath>
#define inf 0x7fffffff
using namespace std;
const int maxn=;
struct b{
int imax,imin;
}tree[maxn*+];
int n,q,x,y,a[maxn+];
void build(int p,int l,int r){
if(l==r) {tree[p].imax=a[l],tree[p].imin=a[l];return ;}
int mid=(l+r)/;
build(p*,l,mid);
build(p*+,mid+,r);
tree[p].imax=max(tree[p*].imax,tree[p*+].imax);
tree[p].imin=min(tree[p*].imin,tree[p*+].imin);
}
int findmin(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imin;
int mid=(l+r)/;
if(x>mid) return findmin(p*+,mid+,r,x,y);
if(y<=mid) return findmin(p*,l,mid,x,y);
return min(findmin(p*+,mid+,r,x,y),findmin(p*,l,mid,x,y));
}
int findmax(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imax;
int mid=(l+r)/;
if(x>mid) return findmax(p*+,mid+,r,x,y);
if(y<=mid) return findmax(p*,l,mid,x,y);
return max(findmax(p*+,mid+,r,x,y),findmax(p*,l,mid,x,y));
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%d%d",&x,&y);
printf("%d\n",findmax(,,n,x,y)-findmin(,,n,x,y));
}
return ;
}
 

最新文章

  1. Chrome 控制台不完全指南
  2. 修改C:\WINDOWS\system32\drivers\etc\hosts 文件有什么作用
  3. Arch Linux Installation Guide
  4. 黄聪:jquery mobile通过a标签页面跳转后,样式丢失、js失效的解决方法
  5. ibatis配置多表关联(一对一、一对多、多对多)
  6. EXT经验--在调试中通过查看handler的第一个参数的xtype得知该参数信息及该handler的归属
  7. 开源 免费 java CMS - FreeCMS2.0 会员我的评论
  8. makefile中使用echo向文件中输出版本号和编译时间
  9. Ionic2开发笔记(2)创建子页面及其应用
  10. h5 做app时和原生交互的小常识。
  11. fs-max、file-nr和nofile的关系
  12. 堆排序HeapSort
  13. 【前端单元测试入门03】Sinon
  14. 2017-12-24 为新语言编写Visual Studio Code语法高亮插件
  15. NodeJS Addon 多线程通信
  16. flask-文件上传
  17. openstack-KVM安装与使用
  18. synchronized无法禁止指令重排序的证明
  19. javascript常见内存泄露
  20. [EXP]Microsoft Windows MSHTML Engine - &quot;Edit&quot; Remote Code Execution

热门文章

  1. php设计模式课程---9、桥接模式是什么
  2. .net中后台c#数组与前台js数组交互
  3. 分享知识-快乐自己:Hibernate 中 get() 和 load()、sava、update、savaOrUpdate、merge,不同之处及执行原理?
  4. google IO大会
  5. Java 吃货联盟
  6. Java_tool_01_Java生成Pdf
  7. Codeforces round 396(Div. 2) 题解
  8. 微软 codeplex 团队
  9. “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第一篇
  10. Guid.NewGuid().ToString()的几种格式 (转)