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 思路:ST表板子题,ST[i][j]表示下表从i到i+2^j-1的最值,查询时,已知l与r,长度len=r-l+1,且2^log2(len)>len/2,令k=log2(len),ST[l][k]肯定超过了长度的一半,反向取后侧,r-m+1=2^len,另一侧就是ST[r-2^k+1][k]
const int maxm = 5e4+;

int Max[maxm][], Min[maxm][], N, Q;

int main() {
scanf("%d%d", &N, &Q);
int t, l, r;
for(int i = ; i <= N; ++i) {
scanf("%d", &t);
Max[i][] = Min[i][] = t;
}
for(int k = ; (<<k) <= N; ++k) {
for(int i = ; i+(<<k)- <= N; ++i) {
Max[i][k] = max(Max[i][k-], Max[i+(<<(k-))][k-]);
Min[i][k] = min(Min[i][k-], Min[i+(<<(k-))][k-]);
}
}
for(int i = ; i < Q; ++i) {
scanf("%d%d", &l, &r);
int k = log((double)(r-l+)) / log(2.0);
printf("%d\n", max(Max[l][k],Max[r-(<<k)+][k]) - min(Min[l][k], Min[r-(<<k)+][k]));
}
return ;
}
												

Day6 - H - Balanced Lineup POJ - 3264的更多相关文章

  1. (线段树)Balanced Lineup --POJ --3264

    链接: 对于POJ老是爆,我也是醉了, 链接等等再发吧! http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/G 只 ...

  2. Balanced Lineup POJ - 3264

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  3. G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题

    题意 给出一个序列  每次查询区间的max-min是多少 思路:直接维护max 和min即可  写两个query分别查最大最小值 #include<cstdio> #include< ...

  4. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  5. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

  6. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  7. G - Balanced Lineup

    G - Balanced Lineup POJ - 3264 思路:水题,线段树的基本操作即可. #include<cstdio> #include<cstring> #inc ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. HA: Infinity Stones-Write-up

    下载地址:点我 哔哩哔哩:点我 主题还是关于复仇者联盟的,这次是无限宝石的. 信息收集 虚拟机的IP为:192.168.116.137 ➜ ~ nmap -sn 192.168.116.1/24 St ...

  2. Hibernate框架:org.hibernate.exception.SQLGrammarException: Cannot open connection at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java92)

    报错信息 org.hibernate.exception.SQLGrammarException: Cannot open connection at org.hibernate.exception. ...

  3. TensorFlow基础二(Shape)

    首先说明tf中tensor有两种shape,分别为static (inferred) shape和dynamic (true) shape,其中static shape用于构建图,由创建这个tenso ...

  4. [ 剑指Offer ] Week2 学习笔记

    打印从1到最大的n位数 题解:由于未知n的大小,需要考虑大数问题.在这样的情况下,逐位地将字符串转换为数字输出,不会有溢出的可能.使用全排列的方式列出所有数字,省去了需要考虑进位的可能. 初始化数组, ...

  5. JS中的原始类型和判断方法

    ECMAScript 中定义了 7 种原始类型: Boolean String Number Null Undefined Symbol(新定义) BigInt(新定义) 注意: 原始类型不包含 Ob ...

  6. day10-Python运维开发基础(函数嵌套、nonlocal声明局部变量、闭包、locals/globals、lambda表达式)

    1. 函数的嵌套与nonlocal 声明局部变量 # ### 函数的嵌套 """ 函数和函数之间可以互相嵌套: 嵌套在内层的叫做内函数 乔涛在外层的叫做外函数 " ...

  7. sentinel控制台

    下载sentinel源码包:https://github.com/alibaba/Sentinel/tree/master,根据自己需要下载不同版本的分支,博主下载得是1.6 下载后解压,然后进入se ...

  8. 解题报告:CF622F

    懒得码字了: 题目链接:CF622F 很简单的数论题,紫题显然是过了些,(不要说... 对于这个式子,是一个\(k+1\)次的多项式,插\(k+2\)次值就好了,烦人的是处理逆元,我的费马小定理显然是 ...

  9. 模板语法(DOM与Vue数据绑定)

    Vue.js使用了基于HTML的模板语法,允许开发者声明式的将DOM绑定至底层Vue实例的数据. 插值 文本:{{ }}数据绑定最常见的形式就是使用Mustache语法(双大括号)的文本插值,解释为普 ...

  10. 「NOIP2011」聪明的质监员

    传送门 Luogu 解题思路 第一眼肯定是没什么思路的 dalao勿喷,但我们仔细看一看式子就会发现 \(Y\) 是随着 \(W\) 的变大而变小的. 所以 \(Y\) 随 \(W\) 的变化是单调的 ...