Balanced Lineup 倍增思想到ST表RMQ
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 36864 | Accepted: 17263 | |
| 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 题目抽象:求数组区间[a,b]之间的最大值与最小值的差。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF=0x4fffffff;
const int MS=; int a[MS];
int minv[MS][];
int maxv[MS][]; int N,Q; void RMQ_init()
{
for(int i=;i<N;i++)
minv[i][]=maxv[i][]=a[i];
for(int j=;(<<j)<=N;j++)
{
for(int i=;i+(<<j)-<N;i++)
{
minv[i][j]=min(minv[i][j-],minv[i+(<<(j-))][j-]);
maxv[i][j]=max(maxv[i][j-],maxv[i+(<<(j-))][j-]);
}
}
} int query_min(int l,int r)
{
int k=;
while((<<(k+))<(r-l+))
k++;
return min(minv[l][k],minv[r-(<<k)+][k]);
} int query_max(int l,int r)
{
int k=;
while((<<(k+))<(r-l+))
k++;
return max(maxv[l][k],maxv[r-(<<k)+][k]);
} int main()
{
scanf("%d%d",&N,&Q);
for(int i=;i<N;i++)
scanf("%d",&a[i]);
RMQ_init();
while(Q--)
{
int l,r;
scanf("%d%d",&l,&r);
l--;
r--;
int max_v=query_max(l,r);
int min_v=query_min(l,r);
printf("%d\n",max_v-min_v);
}
return ;
}
Balanced Lineup 倍增思想到ST表RMQ的更多相关文章
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- POJ 3264 Balanced Lineup(模板题)【RMQ】
<题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...
- poj3264 倍增法(ST表)裸题
打出st表的步骤:1:建立初始状态,2:区间按2的幂从小到大求出值 3:查询时按块查找即可 #include<iostream> #include<cstring> #incl ...
- Find the hotel HDU - 3193 (ST表RMQ)
Summer again! Flynn is ready for another tour around. Since the tour would take three or more days, ...
- hdu2888 二维ST表(RMQ)
二维RMQ其实和一维差不太多,但是dp时要用四维 /* 二维rmq */ #include<iostream> #include<cstring> #include<cs ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- cf689d ST表RMQ+二分
类似hdu5289,但是二分更复杂.本题枚举左端点,右端点是一个区间,需要二分找到区间的左端点和右端点(自己手动模拟一次),然后区间长度就是结果增加的次数 另外结果开long long 保存 /** ...
- hdu6107 倍增法st表
发现lca的倍增解法和st表差不多..原理都是一样的 /* 整篇文章分成两部分,中间没有图片的部分,中间有图片的部分 分别用ST表求f1,f2表示以第i个单词开始,连续1<<j行能写多少单 ...
- [模板]ST表浅析
ST表,稀疏表,用于求解经典的RMQ问题.即区间最值问题. Problem: 给定n个数和q个询问,对于给定的每个询问有l,r,求区间[l,r]的最大值.. Solution: 主要思想是倍增和区间d ...
随机推荐
- QueryInterface
QueryInterface IUnknown *p2; hr = pInnerUnknown->QueryInterface(vGUID2, (void**)&p2); IUnknow ...
- java之四大皆空
SE中的所有空的情况: 第一空:定义变量,变量没有值不能使用,不能打印 public class DiDaJieKong01 { public static void main(String[] ar ...
- hdu1753I Hate It(线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=1754 单点更新,区间求最值 // File Name: hdu1754.cpp // Author: bo_jw ...
- PowerDesigner 表视图修改
PowerDesigner中Table视图同时显示Code和Name,像下图这样的效果: 实现方法:Tools-Display Preference 转自:http://www.shaoqun.com ...
- Java安装程序制作
对于Java桌面应用来说,比较烦琐的就是安装部署问题,如:客户端是否安装有jre.jre版本.jre在哪里下载.如何用jre启动 Java应用等等.不要说刚接触电脑的人,就算是比较熟悉电脑,如果没有接 ...
- 创建类模式(四):原型(Prototype)
定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建 ...
- Oracle闪回
在PLSQL开发时,有时候会遇到对表的误删除,其实遇到这种情况不需要紧张,如果问题较大,请DBA帮忙,如果只是小问题,只需自己处理,利用flashback闪回操作即可,可将表进行恢复 在删除表时,系统 ...
- 【转】解决Fragment already active方法
http://blog.csdn.net/u011730649/article/details/43227721 今天在项目中使用Fragment的时候出现这样的错误: 01-28 10:53:34. ...
- MPIO配置
设置好两块网卡的IP(实用同一段IP,或者不同网段IP均可以配置多路径)iSCSI发起程序配置:1.添加发现(默认设置即可)2.目标-连接-高级:分别配置 本地适配器.发起程序IP.目标门户IP(此处 ...
- ibatis基本内容简介
iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2002年发起的开放源代码项目.于2010年6月16号被 谷歌托管,改名为MyBatis.是一个 ...