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个数之间出现次数最多的是 ...
随机推荐
- nodejs路径处理方法和绝对路径
1. 路径处理方法 __dirname 表示当前文件所在的目录的绝对路径__filename 表示当前文件的绝对路径module.filename ==== __filename 等价process. ...
- 使用canvas画一个雷达效果图的特效代码
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 在eclipse中安装html编辑器插件
1.下载插件( 点击下载) 解压后得到GEF-ALL-3.4.1.zip和tk.eclipse.plugin.htmleditor_2.2.0.jar 2.安装GE ...
- swift的一些东西
.cmd+k 键盘toggle .模拟器的handware设置ios键盘 .设置textfield的return类型为搜索 k.returnKeyType=UIReturnKeyType.search ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- bzoj 1132 [POI2008]Tro 几何
[POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1796 Solved: 604[Submit][Status][Discu ...
- MongoDB入门(3)- MongoDB备份与恢复
1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...
- nodejs与mongo
1.连接URL (使用数据用户名与密码连接或不使用连接数据库) npm install mongodb --save var mon = require('mongodb').MongoClient; ...
- 字符串类dp的题目总结
熟练掌握回文串吧,大致有dp或者模拟类的吧 ①dp+预处理,懂得如何枚举回文串(一) ②dp匹配类型的题目(二) ③dp+预处理 子串类型 (三) ④字符串的组合数(四) 一:划分成回文串 UVA11 ...
- 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题
最近在使用静态库时,总是出现这个问题.下面总结一下我得解决方法: 1. .m文件没有导入 在Build Phases里的Compile Sources 中添加报错的文件 2. .framewor ...