POJ 题目3264 Balanced Lineup(RMQ)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 39046 | Accepted: 18291 | |
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
Source
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b? a:b)
#define min(a,b) (a>b? b:a)
int minv[50050][20],maxv[50050][20];
int a[50050];
void init(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
maxv[i][0]=minv[i][0]=a[i];
}
for(j=1;(1<<j)<=n;j++)
{
for(k=1;k+(1<<j)-1<=n;k++)
{
minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
}
}
}
int q_max(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
}
int q_min(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
init(n);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",q_max(l,r)-q_min(l,r));
}
}
}
POJ 题目3264 Balanced Lineup(RMQ)的更多相关文章
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- poj 3264 Balanced Lineup (RMQ算法 模板题)
RMQ支持操作: Query(L, R): 计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...
- POJ 3264 Balanced Lineup -- RMQ或线段树
一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...
- POJ 3264 Balanced Lineup RMQ ST算法
题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
随机推荐
- mysql event 学习
mysql 通过事件可以实现定时任务 : 1. 检查你的MYSQL是否开了这个功能 show variables like "%scheduler" //注意 最后是er 2. ...
- grep_awk_sed文本处理
小技巧 1.删除0字节文件find -type f -size 0 -exec rm -rf {} \; 2.查看进程按内存从大到小排列ps -e -o “%C : %p : %z : %a”|sor ...
- imx6 PCIE使能加载ath9k无线网卡
imx6q配置pcie无线网卡遇到如下问题: imx6q-pcie 1ffc000.pcie: PCI host bridge to bus 0000:00 pci_bus 0000:00: root ...
- python 小练习1
_input = ['I',6,6,'love','python',6] _str = '' _sum = 0 for item in _input: if isinstance(item,str): ...
- C# 自动注册OCX方法
C#开发系统时,有时候会遇到调用其他语言开发的模块.如果对方提供了OCX时,就需要注册使用,但是实时时,每个客户端都注册一遍就比较麻烦.所以需要系统第一次启动时自动注册OCX. 一:C#注册OCX ...
- linux之参数实用讲解
<1>linux文件参数 在Windows下是使用 %1 %2 %3 而在Linux下是使用 $1 $2 $3 ------------------- 如: 1.某bat文件 cd ...
- Render 使用
Page页面文件,重新Render 方法,目的是把页面的ViewState信息放在最后,利于页面展示速度和SEO优化. Render方法对于重新Html控件还是很好用的. private static ...
- 【UVA10561】Treblecross(SG函数)
题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...
- Array拼接字符串
原文发布时间为:2011-01-12 -- 来源于本人的百度文章 [由搬家工具导入] Array拼接字符串本来就是一种投机取巧的无聊玩意,来源是IE6对字符串的+实现错误一般情况下,如果是语义性的字符 ...
- Android build code command
make bootimage -j8 make systemimage -j8