Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K  
otal Submissions: 52651 Case Time Limit: 2000MS

Description

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

Source

 
 //2017-05-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
int a[N], DPmin[N][], DPmax[N][]; void init(int n)
{
for(int j = ; j<=(int)log2(n); j++)
for(int i = ; i<=n; i++){
DPmax[i][j] = DPmax[i][j-];
DPmin[i][j] = DPmin[i][j-];
if(i+(<<j)- <= n){
DPmax[i][j] = max(DPmax[i][j-], DPmax[i+(<<(j-))][j-]);
DPmin[i][j] = min(DPmin[i][j-], DPmin[i+(<<(j-))][j-]);
}
}
} int query(int l, int r)
{
int k = (int)log2(r-l+);
return max(DPmax[l][k], DPmax[r-(<<k)+][k])-min(DPmin[l][k], DPmin[r-(<<k)+][k]);
} int main()
{
int n, q;
while(scanf("%d%d", &n, &q)!=EOF)
{
for(int i = ; i <= n; i++){
scanf("%d", &DPmax[i][]);
DPmin[i][] = DPmax[i][];
}
init(n);
int l, r;
while(q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", query(l, r));
}
} return ;
}

POJ3264(KB7-G RMQ)的更多相关文章

  1. POJ3264 Balanced Lineup [RMQ模板]

    题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...

  2. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

  3. HDU 5726 GCD (2016 Multi-University Training Contest 1)

      Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description Give y ...

  4. HDU 5869 Different GCD Subarray Query

    离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...

  5. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  6. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  7. 【POJ3264】Balanced Lineup(RMQ)

    题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...

  8. RMQ、POJ3264

    这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...

  9. (WAWAWAWAWAWAW) G. Periodic RMQ Problem

    没有联通门 : Codeforces G. Periodic RMQ Problem /* Codeforces G. Periodic RMQ Problem MMP 什么动态开点线段树啊 ... ...

  10. POJ3264/RMQ

    题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...

随机推荐

  1. PHP进行数据库操作时遇到的一个问题

    PHP进行数据库操作时遇到的一个问题 昨天在进行数据库操作时,遇到了一个问题(用的是 wampserver 环境): <?php $link = @mysqli_connect('localho ...

  2. HttpInvokerUtils

    package com.sprucetec.tms.utils; import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java ...

  3. python 跨平台获取网卡信息和本机ip地址

    笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ...

  4. Docker端口映射(六)

    一.容器端口映射 1.1. 外部访问容器 在启动容器时候,如果不指定参数,在容器外部是无法通过网络来访问容器内的服务的 当容器运行一些网络服务的时候,我们可以通过指定-p或者-P参数来实现能够让外部访 ...

  5. Odoo中使用的数据模型

    Odoo中使用的部分表如下, res_users 用户 res_groups 用户组(角色) res_lang 语言 res_partner 供应商/客户/联系人 res_font 字体 res_co ...

  6. 课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第二周(Optimization algorithms) —— 2.Programming assignments:Optimization

    Optimization Welcome to the optimization's programming assignment of the hyper-parameters tuning spe ...

  7. SqlServer 查看被锁的表和解除被锁的表

    查看被锁的表 1 2 select   request_session_id   spid,OBJECT_NAME(resource_associated_entity_id) tableName   ...

  8. JVM 监控工具 jstack 和 jvisualvm 的使用

    Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...

  9. ListView中的TextView实现跑马灯效果

    1.TextView首先添加android:ellipsize="marquee"   android:marqueeRepeatLimit="marquee_forev ...

  10. 一道JS面试题引发的血案

    刚入职新公司,属于公司萌新一枚,一天下午对着屏幕看代码架构时. BI项目组长给我看了一道面试别人的JS面试题. 虽然答对了,但把理由说错了,照样不及格. 话不多说,直接上题: var a = 1; s ...