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. idea激活,使用破解补丁无需注册码

    idea激活,使用破解补丁无需注册码 2017年08月19日 10:57:54 标签: idea / 破解 / 补丁 / 软件 / 13891 编辑 删除 idea激活,JetBrain旗下软件激活 ...

  2. 区间树Splay——[NOI2005]维护数列

    无指针Splay超详细讲解 区间树这玩意真TM玄学. 学这东西你必须要拥有的 1.通过[模板]文艺平衡树(Splay),[模板]普通平衡树,GSS3 - Can you answer these qu ...

  3. CentOS7配置python3教程

    环境准备: 1.python3.7.1 下载地址:https://www.python.org/    注意:CentOS7默认就有python2的 2.虚拟机CentOS 7 64位,下载地址:ht ...

  4. mongodb插入性能

    转自 https://blog.csdn.net/asdfsadfasdfsa/article/details/60872180 MongoDB与MySQL的插入.查询性能测试     7.1  平均 ...

  5. Struts+Spring+Hibernate整合笔记一

    OpenSessionInview: 1.如果当前方法没有事物环境,则调用完毕getHibernate以后.session关闭: 说明:1如果测试dao层,没有事物环境 2如果测试service层,但 ...

  6. MySQL之约束

    目录 约束(CONSTRAINT) mysql中的约束有哪些? 级联操作 产生的原因: 两种级联的定义方式 约束(CONSTRAINT) 什么是约束? ​ 是一种限制,对某一个东西的限制.例如宪法规定 ...

  7. 6 JavaScript函数&内置构造&函数提升&函数对象&箭头函数&函数参数&参数的值传递与对象传递

    JavaScript函数:使用关键字function定义,也可以使用内置的JavaScript函数构造器定义 匿名函数: 函数表达式可以存储在变量中,并且该变量也可以作为函数使用. 实际上是匿名函数. ...

  8. 十六 OGNL在Struts2环境的入门

    一 配置核心过滤器

  9. 为spring boot 写的Controller中的rest接口配置swagger

    1.pom.xml文件中加入下列依赖: <dependency> <groupId>io.springfox</groupId> <artifactId> ...

  10. 手机wifi的mac地址是什么??

    简单来说,每个能够接入网络的设备,无论是平板.手机.电脑.电视都有一个专门的序号,这个序号就被称为MAC,正常来说可以看做是这款设备的唯一标识,手机里的MAC其实是特指Wi-Fi无线网卡的MAC地址. ...