http://poj.org/problem?id=3264

Time Limit: 5000MS     Memory Limit: 65536K

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 ≤ A ≤ B ≤ N), 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


Sample Output


Source

 
 
题意:
一个农夫有N头牛,每头牛的高度不同,我们需要找出区间[a,b]中最高的牛和最低的牛的高度差。
 
题解:
明显是区间最值问题,线段树和RMQ
 
线段树做法(耗时2.2s):
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,q;
struct node
{
int l;
int r;
int MAX;
int MIN;
}SegTree[<<]; void PushUp(int rt)
{
SegTree[rt].MAX=max(SegTree[rt<<].MAX,SegTree[rt<<|].MAX);
SegTree[rt].MIN=min(SegTree[rt<<].MIN,SegTree[rt<<|].MIN);
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
if(l==r)
{
scanf("%d",&SegTree[rt].MAX);
SegTree[rt].MIN=SegTree[rt].MAX;
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
PushUp(rt);
} int Query_MAX(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MAX;
}
int MAX=;
int mid=(l+r)>>;
if(L<=mid)
MAX=max(MAX,Query_MAX(L,R,rt<<));
if(R>mid)
MAX=max(MAX,Query_MAX(L,R,rt<<|));
return MAX;
} int Query_MIN(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MIN;
}
int MIN=INF;
int mid=(l+r)>>;
if(L<=mid)
MIN=min(MIN,Query_MIN(L,R,rt<<));
if(R>mid)
MIN=min(MIN,Query_MIN(L,R,rt<<|));
return MIN;
} int main()
{
scanf("%d %d",&n,&q);
Build(,n,);
for(int i=;i<=q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",Query_MAX(a,b,)-Query_MIN(a,b,));
}
return ;
}
RMQ做法:
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
using namespace std; const int N = ;
int FMAX[N][], FMIN[N][]; void RMQ(int n)
{
for(int j = ; j != ; ++j)
{
for(int i = ; i <= n; ++i)
{
if(i + ( << j) - <= n)
{
FMAX[i][j] = max(FMAX[i][j - ], FMAX[i + ( << (j - ))][j - ]);
FMIN[i][j] = min(FMIN[i][j - ], FMIN[i + ( << (j - ))][j - ]);
}
}
}
} int main()
{
int num, query;
int a, b;
while(scanf("%d %d", &num, &query) != EOF)
{
for(int i = ; i <= num; ++i)
{
scanf("%d", &FMAX[i][]);
FMIN[i][] = FMAX[i][];
}
RMQ(num);
while(query--)
{
scanf("%d%d", &a, &b);
int k = (int)(log(b - a + 1.0) / log(2.0));
int maxsum = max(FMAX[a][k], FMAX[b - ( << k) + ][k]);
int minsum = min(FMIN[a][k], FMIN[b - ( << k) + ][k]);
printf("%d\n", maxsum - minsum);
}
}
return ;
}
 
最后推荐一篇写的挺好的一篇博客:https://blog.csdn.net/weixin_43272781/article/details/83443310
 
 
 
 

POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)的更多相关文章

  1. POJ 3264 Balanced Lineup 区间最值

    POJ3264 比较裸的区间最值问题.用线段树或者ST表都可以.此处我们用ST表解决. ST表建表方法采用动态规划的方法, ST[I][J]表示数组从第I位到第 I+2^J-1 位的最值,用二分的思想 ...

  2. poj 3264 Balanced Lineup 区间极值RMQ

    题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...

  3. POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53721   Accepted: 25244 ...

  4. POJ 3264 Balanced Lineup(模板题)【RMQ】

    <题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...

  5. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  6. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  7. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  8. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

  9. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  10. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. windows 2008R2 搭建web实现https访问

    一.安装服务. 二.IIS创建证书申请. 三.ca颁发证书 三.创建网站. 创建一个新建文件夹,并在文件夹中创建一个txt文件. 把新建文本文档.txt重命名为index.html 完成证书申请(导入 ...

  2. CSS绘制小三角

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. RectTransform详解

    乾坤那个大挪移   ----------------------------------------------------------------- 我是分割线 ------------------ ...

  4. Day 9:双列集合Map及实现该接口的类的常用方法

    为什么要学双列集合? 因为单列集合无法处理映射关系,会有成对出现的数据 Map接口  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复 Map接 ...

  5. Mac Eclipse 打包可执行jar文件

    2 3 4 保存后 终端 cd 目录 java -jar xxxx.jar

  6. 阿里P7Java最全面试296题:阿里天猫、蚂蚁金服含答案文档解析

    [阿里天猫.蚂蚁.钉钉面试专题题目加答案] 不会做别着急:文末有答案以及视频讲解,架构师资料 1. junit用法,before,beforeClass,after, afterClass的执行顺序 ...

  7. 浅谈无字母数字构造webshell

    0x00 问题 <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $_GET['code']; if(strlen($cod ...

  8. 高级css效果

    1.图片渐变效果 background linear-gradient(top,rgba(0,0,0,.8),rgba(0,0,0,.8))

  9. 读书笔记 - js高级程序设计 - 第五章 引用类型

      引用类型 和 类 不是一个概念 用typeof来检测属性是否存在 typeof args.name == "string"  需要实验 访问属性的方法 .号和[] 一般情况下要 ...

  10. 桥接 brctl

    把eth0和wlan0桥接在一起 作用:测试wlan0网卡的并发性能   两个网卡桥接后把linux主机模拟成一个“无线路由交换机” Vi   br0.sh #!/bin/bash ifconfig ...