poj 3264(RMQ或者线段树)
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
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
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或者线段树)的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- 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 ...
- POJ 3264 Balanced Lineup -- RMQ或线段树
一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
随机推荐
- 数据添加到solr索引库后前台如何搜索
主要结构: 查询 Dao: package com.taotao.search.dao.impl; import java.util.ArrayList; import java.util.List; ...
- Trades FZU - 2281 (大数+贪心)
This is a very easy problem. ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n ...
- Android提示框与通知的使用
1.通知 Android 3.0以前使用的方法 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION ...
- ARM指令集、Thumb指令集、Thumb-2指令集
MCU使用什么指令集主要由内核决定的,比如Cortex-M3使用的是Thumb-2指令集 ARM指令集: 编代码全部是 32bits 的,每条指令能承载更多的信息,因此使用最少的指令完成功能, 所以在 ...
- Hadoop Yarn事件处理框架源码分析
由于想在项目中使用类似yarn的事件处理机制,就看了实现.主要是由Dispatcher.java,EventHandler.java,Service.java这3个类撑起来的. 在事件处理之前,先注册 ...
- EasyUI Tree递归方式获取JSON
最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...
- Jenkins有用的插件
1. Multijob plugin: 多个任务同时运行 2. ssh slave plugin: 用于安装slave? Allows to launch over ssh, using a java ...
- 【hdu5217-括号序列】线段树
题意:给一串括号,有2个操作,1.翻转某个括号.2.查询某段区间内化简后第k个括号是在原序列中的位置.1 ≤ N,Q ≤ 200000. 题解: 可以知道,化简后的序列一定是)))((((这种形式的. ...
- 「6月雅礼集训 2017 Day8」gcd
[题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ...
- Bzoj4481 [Jsoi2015]非诚勿扰
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 147 Solved: 75 Description [故事背景] JYY赶上了互联网创业的大潮,为非 ...