Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 42929   Accepted: 20184
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 ≤ ABN), 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

题意:区间最大值与最小值之差RMQ版:(不懂的可以参考blog)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010 int a[N];
int max_dp[N][];
int min_dp[N][];
int MAX(int i,int j){
if(i>=j) return i;
return j;
}
int MIN(int i,int j){
if(i<=j) return i;
return j;
}
void init_MAX_RMQ(int n){
for(int i=;i<=n;i++) max_dp[i][]=a[i];
for(int j=;(<<j)<=n;j++){
for(int i=;i<=n-(<<j)+;i++){
///F[i, j]=max(F[i,j-1], F[i + 2^(j-1),j-1])。
max_dp[i][j] = MAX(max_dp[i][j-],max_dp[i+(<<(j-))][j-]);
}
}
}
int MAX_RMQ(int a,int b){
int k = (int)(log(b-a+1.0)/log(2.0));
///RMQ(A, i, j)=min{F[i,k],F[j-2^k+1,k]}
return MAX(max_dp[a][k],max_dp[b-(<<k)+][k]);
}
void init_MIN_RMQ(int n){
for(int i=;i<=n;i++) min_dp[i][]=a[i];
for(int j=;(<<j)<=n;j++){
for(int i=;i<=n-(<<j)+;i++){
min_dp[i][j] = MIN(min_dp[i][j-],min_dp[i+(<<(j-))][j-]);
}
}
}
int MIN_RMQ(int a,int b){
int k = (int)(log(b-a+1.0)/log(2.0));
return MIN(min_dp[a][k],min_dp[b-(<<k)+][k]);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
init_MAX_RMQ(n);
init_MIN_RMQ(n);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",MAX_RMQ(a,b)-MIN_RMQ(a,b));
}
}
return ;
}

线段树:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010 struct Tree{
int l,r;
int Max,Min;
}tree[*N];
int a[N];
int MAX_VALUE;
int MIN_VALUE;
int MAX(int i,int j){
if(i>=j) return i;
return j;
}
int MIN(int i,int j){
if(i<=j) return i;
return j;
}
void PushUp(int idx){
tree[idx].Max = MAX(tree[idx<<].Max,tree[idx<<|].Max);
tree[idx].Min = MIN(tree[idx<<].Min,tree[idx<<|].Min);
}
void build(int l,int r,int idx){
tree[idx].l = l;
tree[idx].r = r;
if(l==r) {
tree[idx].Max = tree[idx].Min = a[l];
return ;
}
int mid=(l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
PushUp(idx);
}
void query(int l,int r,int idx){
if(tree[idx].l==l&&tree[idx].r==r){
MAX_VALUE = MAX(MAX_VALUE,tree[idx].Max);
MIN_VALUE = MIN(MIN_VALUE,tree[idx].Min);
return;
}
int mid=(tree[idx].l+tree[idx].r)>>;
if(mid>=r) query(l,r,idx<<);
else if(mid<l) query(l,r,idx<<|);
else{
query(l,mid,idx<<);
query(mid+,r,idx<<|);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
while(m--){
int b,c;
scanf("%d%d",&b,&c);
MAX_VALUE=-;
MIN_VALUE=;
query(b,c,);
printf("%d\n",MAX_VALUE-MIN_VALUE);
}
}
return ;
}

poj 3264(RMQ或者线段树)的更多相关文章

  1. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  2. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  3. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  4. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  5. POJ 3264 Balanced Lineup 线段树 第三题

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

  6. POJ 3264 Balanced Lineup (线段树)

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

  7. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

  8. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

  9. POJ 3368 Frequent values 线段树与RMQ解法

    题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...

随机推荐

  1. 破解wingide编辑器

    先到官网下载最新版的wingide(我下载的是5.1.11-1),然后安装,打开,出现下面的界面时选第三个,然后输入“ENX27-HWM6G-XYVFA-165PG”,如下图所示: 接下来你软件会给你 ...

  2. saltstack:multi-master configuration

    官方手册地址:http://docs.saltstack.com/topics/tutorials/multimaster.html 总结起来,有以下几步: Create a redundant ma ...

  3. Django请求原理(二)

    1,Web服务器(中间件)收到一个http请求 2,Django在URLconf里查找对应的视图(View)函数来处理http请求 3,视图函数调用相应的数据模型来存取数据.调用相应的模板向用户展示页 ...

  4. oepncv-学习笔记一

    安装opencv文件时若需要cmake编译,如果中间出现 解决办法是: 在opencv的文件中找到包含cmakelist.txt的文件夹,把where is the source code:的路径改成 ...

  5. jQuery对象初始化的传参方式

    jQuery对象初始化的传参方式包括: 1.$(DOMElement) 2.$(' ... '), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第 ...

  6. [洛谷P1822] 魔法指纹

    洛谷题目连接:魔法指纹 题目描述 对于任意一个至少两位的正整数n,按如下方式定义magic(n):将n按十进制顺序写下来,依次对相邻两个数写下差的绝对值.这样,得到了一个新数,去掉前导0,则定义为ma ...

  7. 嵌入式Nosql数据库——LiteDB

    LiteDB是一个开源的 .NET 开发的小型快速轻量级的 NoSQL 嵌入式数据库,特性: 无服务器的 NoSQL 文档存储,数据存储在单一文件中类似 MongoDb 的简单 API100% C# ...

  8. 【Foreign】不等式 [数论]

    不等式 Time Limit: 10 Sec  Memory Limit: 128 MB Description 小z热衷于数学. 今天数学课的内容是解不等式:L<=S*x<=R .小z心 ...

  9. 【51NOD-0】1006 最长公共子序列Lcs

    [算法]经典DP [题解]经典lcs,输出路径可以记录上一个有效节点就是有点麻烦. 因为开始时写法不太明确,打印结果时初始循环地方搞错了,后来修正写法时忘了改过来,调了好久. #include< ...

  10. charles https抓包

    1. 配置 Charles 根证书 首先打开 Charles: Charles 启动界面 主界面 然后如下图操作:   之后会弹出钥匙串,如果不弹出,请自行打开钥匙串,如下图: 钥匙串 系统默认是不信 ...