Balanced Lineup

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

USACO 2007 January Silver
题目大意:

描述

每天挤奶,农民约翰的N牛(1≤N≤50000)总是在相同的顺序排列。一天农夫约翰决定组织一场极限飞盘的奶牛。为简单起见,他将一个连续的范围的奶牛挤奶阵容来玩这个游戏。然而,对于所有的奶牛很有趣他们不应该太多不同的高度。

农民约翰列了一个清单,问(1 Q≤≤200000)潜在的牛组和他们的高度(1≤高度≤1000000)。对于每一组,他想要你的帮助确定最短之间的高度差和最高的牛。

输入

1号线:两个空格分隔的整数,N和Q。
行2 . .N + 1:i + 1行包含一个整数,牛我的高度
行N + 2 . .N + Q + 1:A和B两个整数(1≤≤B≤N),代表牛从A到B包容的范围。

输出

行1 . .问:每一行包含一个整数,响应回复和显示之间的高度差最高和最短的牛的范围。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>using namespace std;
const int N=200001;int n,m;
int a[N],maxrmq[N][51],minrmq[N][51];inline int read()
{int x=0;int f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();    }while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x*f;
}int rmqmax(int l,int r)
{int k(0);while(l+(1<<(k+1))<=r+1)k++;return max(maxrmq[l][k],maxrmq[r-(1<<k)+1][k]);
}int rmqmin(int l,int r)
{int k(0);while(l+(1<<(k+1))<=r+1)k++;return min(minrmq[l][k],minrmq[r-(1<<k)+1][k]);
}int main()
{n=read(),m=read();for(int i=1;i<=n;i++)a[i]=read();for(int i=1;i<=n;i++)maxrmq[i][0]=minrmq[i][0]=a[i];for(int j=1;j<=25;j++)for(int i=1;i+(1<<j)<=n+1;i++)maxrmq[i][j]=max(maxrmq[i][j-1],maxrmq[i+(1<<(j-1))][j-1]),minrmq[i][j]=min(minrmq[i][j-1],minrmq[i+(1<<(j-1))][j-1]);for(;m;m--){int x=read(),y=read();printf("%d\n",rmqmax(x,y)-rmqmin(x,y));}return 0;
}

转载于:https://www.cnblogs.com/lyqlyq/p/7147716.html

poj Balanced Lineup RMQ相关推荐

  1. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  2. POJ 3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 24349   Accepted: 11348 ...

  3. POJ 3264 Balanced Lineup (RMQ)

    Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...

  4. POJ 3264 Balanced Lineup

    POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...

  5. POJ 3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53629   Accepted: 25223 ...

  6. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  7. bzoj 1636 bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队(RMQ)

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1514  Solved:  ...

  8. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

  9. poj3264 - Balanced Lineup(RMQ_ST)

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

最新文章

  1. Fins TCP协议
  2. checkpoint技术mysql_认识MySQL中的Checkpoint技术
  3. MyEclipse导入新项目后,不能发布到Tomcat
  4. Android二维码之创建
  5. [网络开发]同步与线程安全方案
  6. Android访问瓦片地图 费流量,瓦片地图服务在线资源访问总结
  7. 查找算法(一)顺序查找
  8. treeset 是有序吗_TreeSet如何实现有序?
  9. java swarm_java for swarm 学习笔记4
  10. 让 “微软雅黑” 在IE6下完美显示
  11. Java网上商城系统_JavaWeb源码网上商城系统
  12. html5 canvas实现led样式数字字体
  13. java基础热门侠客养成_侠客养成手册攻略大全 新手攻略开局任务流程汇总[多图]...
  14. Mac入门指南(二)常用命令
  15. 初窥 Python 的 import 机制
  16. 考拉验数(自动化验数)设计方案和实现
  17. 这些跳槽理由千万不能说!
  18. 天文竞赛怎么用计算机,师范生教学技能大赛!计算机设计大赛!主持礼仪风采大赛!天文台活动预告!民宿企划竞赛!舞蹈大赛!...
  19. 《从前慢》 ----- 作者:木心
  20. 【科研数据处理】[基础]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)

热门文章

  1. Systrace的用法小结
  2. ListView实现多种item布局的方法和注意事项
  3. vs2008生成lib文件
  4. ASP.Net新手项目经验谈
  5. 网页中Flash播放器常用参数设置(转)
  6. 电脑显示没有被指定在上运行_电脑出现“**.dll没有被指定在Windows上运行”提示怎么解决...
  7. openstack开发_在OpenStack开发中有效使用指标
  8. wms策略文档_内容策略:技术文档的新理念
  9. (14)Node.js 核心模块—http
  10. Java常见问题(1)navicat连接mysql报2059错误