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. hdoj6703 2019 CCPC网络选拔赛 1002 array

    题意 description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the ...

  2. SpringBoot+JWT+SpringSecurity+MybatisPlus实现Restful鉴权脚手架

    若图片查看异常,请前往掘金查看:https://juejin.im/post/5d1dee34e51d4577790c1cf4 前言 JWT(json web token)的无状态鉴权方式,越来越流行 ...

  3. ElementUI 日期选择器 datepicker 选择范围限制

    在使用elementUI中日期选择器时,经常会遇到这样的需求——对可选择的时间范围有一定限制,比如我遇到的就是:只能选择今天以前的一年以内的日期. 查阅官方文档,我们发现它介绍的并不详细,下面我们就来 ...

  4. Maven 项目中使用 logback

    添加依赖 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logsta ...

  5. springboot 模板

    参考:https://blog.csdn.net/wangb_java/article/details/71775637

  6. 第1节 Scala基础语法:13、list集合的定义和操作;16、set集合;17、map集合

    list.+:5 , list.::5: 在list集合头部添加单个元素5 : li1.:+(5):在list集合尾部添加单个元素5: li1++li2,li1:::li2:在li1集合尾部添加il2 ...

  7. 学习java时在要求输出的数字带俩个小数点时,利用String.format时出现的问题

    public class StringFormatDemo { public static void main(String[] args) { //String.format 实现了四舍五入 Sys ...

  8. rhel7 系统服务——unit(单元)

    Linux内核版本从3.10后开始使用systemd管理服务,这也是系统开机后的第一个服务.systemd通过unit单元文件来管理服务. 它保存了服务.设备.挂载点和操作系统其他信息的配置文件,并能 ...

  9. Python 基础之面向对象类的继承与多态

    一.继承 定义:一个类除了拥有自身的属性方法之外,还拥有另外一个类的属性和方法继承: 1.单继承 2.多继承子类: 一个类继承了另外一个类,那么这个类是子类(衍生类)父类:一个类继承了另外一个类,被继 ...

  10. 使用 JvisualVM 监控 spark executor

    使用 JvisualVM,需要先配置 java 的启动参数 jmx 正常情况下,如下配置 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmx ...