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 线段树维护最大最小,不涉及更改,只用pushup query就可以了
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
int minn[maxn<<],maxx[maxn<<],n,q,a[maxn],maxpos,minpos; void pushup(int rt) {
minn[rt]=min(minn[rt<<],minn[rt<<|]);
maxx[rt]=max(maxx[rt<<],maxx[rt<<|]);
} void build(int rt,int L,int R){
minn[rt]=;
maxx[rt]=;
if(L==R) {
scanf("%d",&a[rt]);
minn[rt]=maxx[rt]=a[rt];
return;
}
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
pushup(rt);
} void query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) {
minpos=min(minpos,minn[rt]);
maxpos=max(maxpos,maxx[rt]);
return;
}
int mid=(L+R)>>;
if(l<=mid) query(rt<<,L,mid,l,r);
if(r>mid) query(rt<<|,mid+,R,l,r);
} int main() {
while(~scanf("%d%d",&n,&q)) {
build(,,n);
int l,r;
while(q--) {
maxpos=-,minpos=inf;
scanf("%d%d",&l,&r);
query(,,n,l,r);
printf("%d\n",l==r?:maxpos-minpos);
}
}
}
												

最新文章

  1. NGUI BUG /各种坑
  2. (转) 关于Oracle EBS邮件服务无法使用的报错
  3. $q服务的API详解
  4. &lt;context:annotation-config&gt; 跟 &lt;context:component-scan&gt;诠释及区别
  5. 图解TCP/IP读书笔记(一)
  6. I.MX6 android 获取framebuffer信息
  7. APUE(3)——文件I/O
  8. 非spring环境中配置文件工具
  9. SQL 建立临时表进行查询
  10. 使用file_get_content系列函数和使用curl系列函数采集图片的性能对比
  11. Linux下nc命来实现文件传输
  12. Python--socketserve源码分析(一)
  13. Linux背背背(6)
  14. VM虚拟机Linux和主机数据传输
  15. Django 笔记(四)模板标签 ~ 自定义过滤器
  16. [转] 理解Object.defineProperty的作用
  17. 本地文件上传GitHub
  18. Python 核心编程
  19. Mac XMind8 保存时报错
  20. PyTorch 数据集类 和 数据加载类 的一些尝试

热门文章

  1. nginx isis
  2. Android 4学习(4):概述 - Using Resources
  3. CSS——position
  4. c#.net常用字符串函数 字符串常用方法 string
  5. Elasticsearch之curl创建索引
  6. Ajax笔记(二)
  7. actionbar中添加searchview并监听期伸缩/打开的方法
  8. 并发之AtomicInteger
  9. 【总结整理】关于ArcGIS中拓扑的理解
  10. 100725B Banal Tickets