POJ——3264Balanced Lineup(RMQ模版水题)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 44112 | Accepted: 20713 | |
| 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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
const int N=50010;
int n,q;
int maxm[N][18],minm[N][18];
void rmq()
{
int i,j;
for (j=1; (1<<j)<=n; j++)
{
for (i=1; i+(1<<j)-1<=n; i++)
{
minm[i][j]=min(minm[i][j-1],minm[i+(1<<(j-1))][j-1]);
maxm[i][j]=max(maxm[i][j-1],maxm[i+(1<<(j-1))][j-1]);
}
}
}
int main(void)
{
int i,j,l,r;
while (~scanf("%d%d",&n,&q))
{
MM(maxm);MM(minm);
for (i=1; i<=n; i++)
{
scanf("%d",&maxm[i][0]);
minm[i][0]=maxm[i][0];
}
rmq();
for (i=0; i<q; i++)
{
scanf("%d%d",&l,&r);
int k=(int)log2(r-l+1);
int ans=max(maxm[l][k],maxm[r-(1<<k)+1][k])-min(minm[l][k],minm[r-(1<<k)+1][k]);
printf("%d\n",ans);
}
}
return 0;
}
POJ——3264Balanced Lineup(RMQ模版水题)的更多相关文章
- poj 3080 Blue Jeans(水题 暴搜)
		
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
 - POJ 3984 - 迷宫问题 - [BFS水题]
		
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
 - poj 1007:DNA Sorting(水题,字符串逆序数排序)
		
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
 - poj 1004:Financial Management(水题,求平均数)
		
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
 - POJ 3176 Cow Bowling (水题DP)
		
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
 - poj 1658 Eva's Problem(水题)
		
一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...
 - poj 3264 Balanced Lineup (RMQ算法 模板题)
		
RMQ支持操作: Query(L, R): 计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...
 - ACM: POJ 1401 Factorial-数论专题-水题
		
POJ 1401 Factorial Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
 - POJ 1986  DIstance Query LCA水题
		
给出一棵树,对于每一个询问,给出2个节点,输出2个节点的距离. 输入中有字母,那个是没有用的,不用管. 思路: 0.选择编号为1的节点作为树的root (注意:有些题的边是单向的,这时候我们要根据节点 ...
 
随机推荐
- 日常-acm-韩信点兵
			
相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排,五人一排,七人一排地变换队形,而他每次只看一眼队伍的排尾就知道人数了.输入包含多组数据,每组数据包含三个非负整数a,b,c,表示 ...
 - angular设置反向代理
			
本地调试,需要用到服务器的api,发现chrome安全问题,需要解决跨域问题.现给出解决方案: 1.增加proxy.conf.json文件 位置与package.json文件同级(可指定) 2.pac ...
 - DROP INDEX - 删除一个索引
			
SYNOPSIS DROP INDEX name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP INDEX 从数据库中删除一个现存的索引. 要执 ...
 - axios的post请求方法---以Vue示例
			
Axios向后端提交数据的参数格式是json,而并非用的是form传参,post表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,而使 ...
 - 修改Windows默认调试器
			
程序崩溃时,系统会弹窗让你选择是否进行调试,可以设置系统默认调试器. 注册表位置: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVe ...
 - javaweb基础(24)_jsp一般的标签开发
			
一.标签技术的API 1.1.标签技术的API类继承关系 二.标签API简单介绍 2.1.JspTag接口 JspTag接口是所有自定义标签的父接口,它是JSP2.0中新定义的一个标记接口,没有任何属 ...
 - redis的一个bug
			
清楚redis缓存的时候,出现以下问题: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not ...
 - 使用js将div高度设置为100%
			
在开发的工程中使用到了一些开源的bootstrap模板进行开发,在遇到一些需要替换的内容部分部分时,经常出现高度设置100%无法生效的问题,这里来用js强行设置一下. 思路:js监听窗口的缩放 ...
 - Linux菜鸟起飞之路【七】文件合并、归档和压缩
			
一.文件合并操作 1.覆盖符号与追加符号 a)“>”代表将左边文件的内容覆盖右边文件的内容,如果右边文件不存在则创建这个文件 b)“>>”代表将左边文件的内容追加到右边文件中,如果右 ...
 - Python数据分析实战视频教程【小蚊子数据分析实战课程】
			
点击了解更多Python课程>>> Python数据分析实战视频教程[小蚊子数据分析实战课程] [课程概述] Python数据分析实战' 适用人群:适合需提升竞争力.提升工作效率.喜 ...