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. 番外篇!全球首个微信应用号开发教程!小程序 DEMO 视频奉上!

    大家好,我是博卡君.经过国庆节的七天假期,相信很多朋友都已经研究出自己的小程序 demo 了吧?我最近也利用休息时间关注了一下网上关于小程序开发的讨论,今天就利用这个番外篇谈谈自己对小程序的一些想法吧 ...

  2. HTML5中改变了哪些东西?

    HTML5 推出的理由 想要把目前web上存在的各种问题一并解决 Web浏览器之间的兼容性很低 文档结构不够明确 Web应用程序的功能受到了限制 HTML5重新定义了浏览器的统一标准 HTML5 与 ...

  3. 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)

    创建web项目,引入jar包 引入Spring配置文件

  4. windows 以太坊开发框架Truffle环境搭建

    https://www.jianshu.com/p/f7a4de0cba9d 一.安装DApp开发环境 1.1 安装Node.js 我们使用官方长期支持的8.10.0LTS版本,下载64位包装包. 下 ...

  5. 1.requests+正则表达式爬猫眼电影TOP100

    import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...

  6. ASP.NET MVC4 Web项目中使用Log4Net记录日志到文件和数据库。

    下载与.netframework版本向对应的log4net.dll ,然后添加引用.下载地址:http://logging.apache.org/log4net/download_log4net.cg ...

  7. 解压Assets.car获取App中的图片资源

    iOS开发时图片资源(png)是放置在Images.xcassets文件夹中进行管理的. 项目归档后就是ipa文件,在ipa文件中,Images.xcassets文件夹的内容放置在了Assets.ca ...

  8. OC中NSLog函数输出格式罗列

    格式 类型 格式 类型 格式 类型 %@ 对象 %d,%i 整数 %u 无符整数 %f 浮点 %x,%X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e 浮点(科学计算) %g ...

  9. openstack的一台Nova主机上的虚拟机网络的配置

    1.一台虚拟机器的网络配置,通过openstack/nova计算节点服务生成的虚拟机配置文件 <interface type='bridge'> <mac address='fa:1 ...

  10. 7. 通过JDBC源码来分析线程上下文类加载器以及SPI的使用

    目录 1. 什么是全盘负责委托机制 2. 为什么需要有线程上下文类加载器 2.1 使用JDBC的例子,分析为什么双亲委托机制不能实现要求 2.2 线程上下文类加载器的作用 3. 线程上下文类加载器的使 ...