B. Balanced Lineup

Time Limit: 5000ms
Case Time Limit: 5000ms
Memory Limit: 65536KB
 
64-bit integer IO format: %lld      Java class name: Main
 

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 解题:RMQ
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int mn[][],mx[][],d[];
int main(){
int n,m,x,y,i,j;
while(~scanf("%d %d",&n,&m)){
for(i = ; i < n; i++){
scanf("%d",d+i);
}
memset(mn,,sizeof(mn));
memset(mx,,sizeof(mx));
for(i = n-; i >= ; i--){
mn[i][] = mx[i][] = d[i];
for(j = ; i+(<<j)- < n; j++){
mn[i][j] = min(mn[i][j-],mn[i+(<<(j-))][j-]);
mx[i][j] = max(mx[i][j-],mx[i+(<<(j-))][j-]);
}
}
for(i = ; i < m; i++){
scanf("%d %d",&x,&y);
if(x > y) swap(x,y);
int r = y - x + ;
r = log2(r);
int theMax,theMin;
theMax = max(mx[x-][r],mx[y-(<<r)][r]);
theMin = min(mn[x-][r],mn[y-(<<r)][r]);
printf("%d\n",theMax-theMin);
}
}
return ;
}

B. Balanced Lineup的更多相关文章

  1. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  2. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  3. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  4. poj 3264 Balanced Lineup (RMQ)

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

  5. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  6. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John ...

  7. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  8. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  9. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  10. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

随机推荐

  1. js filter用法比较

    讲解一个很实用的JS小语法 filter 就是从数组中找到适合条件的元素(比如说大于某一个元素的值) var arr=[1,23,5,78,34,55,13]; 如何才能找到大于23的所有元素呢, 1 ...

  2. Mysql order by 多字段排序

    mysql单个字段降序排序: select * from table order by id desc; mysql单个字段升序排序: select * from table order by id ...

  3. Java类的静态块の一

    类的静态块在类加载时候执行,执行早于构造函数,并且只执行一次. 下面这个例子可以帮助理解: package untility; public class A { // 静态块 static { A c ...

  4. 自动完成文本框(AutoCompleteTextView与MultiAutoCompleteTextView)关联适配器

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  5. 运行powershell 脚本 在此系统上禁止运行脚本

    解决方法: 首次在计算机上启动 Windows PowerShell 时,现用执行策略很可能是 Restricted(默认设置). Restricted 策略不允许任何脚本运行. 若要了解计算机上的现 ...

  6. HDU 6166 Senior Pan(多校第九场 二进制分组最短路)

    题意:给出n个点和m条有向边(有向边!!!!我还以为是无向查了半天),然后给出K个点,问这k个点中最近的两点的距离 思路:比赛时以为有询问,就直接丢了,然后这题感觉思路很棒,加入把所有点分成起点和终点 ...

  7. python之数据类型补充

    1. capitalize (首字母大写) 例题: s = "alex wusir" s1 = s.capitalize() # 格式 print(s1) ''' 输出结果 Ale ...

  8. Linux C++/C开发所必需的一系列工具

    系统平台下的开发工具.开发环境各有不同.Linux C++/C开发所必需的一系列工具: 1. vi(vim)文本编辑器一个UNIX世界标准的文本编辑器,简约而强大,不论作为开发人员还是系统管理员,熟练 ...

  9. tomcat中如何禁止和允许主机或地址访问

    1.tomcat中如何禁止和允许列目录下的文件 在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下: <servlet>...< ...

  10. xheditor的实例程序—类似word的编辑器

    编辑器工具栏:类似word的编辑器 1.1.下载,兼容性 xhEditor官方网站地址为:http://xheditor.com/,打开右上角的免费下载 | 参数向导链接,即可找到最新版本的下载地址. ...