题意:求区间最大值-最小值。

分析:

1、线段树

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 50000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int minv[MAXN << 2], maxv[MAXN << 2];
int _min, _max;
void build(int id, int L, int R){
if(L == R){
minv[id] = maxv[id] = a[L];
}
else{
int mid = L + (R - L) / 2;
build(id << 1, L, mid);
build(id << 1 | 1, mid + 1, R);
minv[id] = min(minv[id << 1], minv[id << 1 | 1]);
maxv[id] = max(maxv[id << 1], maxv[id << 1 | 1]);
}
}
void query(int l, int r, int id, int L, int R){
if(l <= L && R <= r){
_max = max(_max, maxv[id]);
_min = min(_min, minv[id]);
return;
}
int mid = L + (R - L) / 2;
if(l <= mid){
query(l, r, id << 1, L, mid);
}
if(r > mid){
query(l, r, id << 1 | 1, mid + 1, R);
}
}
int main(){
int N, Q;
scanf("%d%d", &N, &Q);
for(int i = 1; i <= N; ++i){
scanf("%d", &a[i]);
}
build(1, 1, N);
while(Q--){
int A, B;
scanf("%d%d", &A, &B);
_min = INT_INF;
_max = 0;
query(A, B, 1, 1, N);
printf("%d\n", _max - _min);
}
return 0;
}

2、RMQ

Sparse-Table算法,预处理时间O(nlogn),查询O(1)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 50000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int N, Q;
int minv[MAXN][20];
int maxv[MAXN][20];
void RMQ_init(){
for(int i = 1; i <= N; ++i){
minv[i][0] = maxv[i][0] = a[i];
}
for(int j = 1; (1 << j) <= N; ++j){
for(int i = 1; (i + (1 << j) - 1) <= N; ++i){
minv[i][j] = min(minv[i][j - 1], minv[i + (1 << (j - 1))][j - 1]);
maxv[i][j] = max(maxv[i][j - 1], maxv[i + (1 << (j - 1))][j - 1]);
}
}
}
int RMQ(int L, int R){
int k = 0;
while((1 << (k + 1)) <= (R - L + 1)) ++k;
return max(maxv[L][k], maxv[R - (1 << k) + 1][k]) - min(minv[L][k], minv[R - (1 << k) + 1][k]);
}
int main(){
scanf("%d%d", &N, &Q);
for(int i = 1; i <= N; ++i){
scanf("%d", &a[i]);
}
RMQ_init();
while(Q--){
int A, B;
scanf("%d%d", &A, &B);
printf("%d\n", RMQ(A, B));
}
return 0;
}

  

POJ - 3264 Balanced Lineup(线段树或RMQ)的更多相关文章

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

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

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

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

  3. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

  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 ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  8. Poj 3264 Balanced Lineup RMQ模板

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

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

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

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

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

随机推荐

  1. sparkRDD:第4节 RDD的依赖关系;第5节 RDD的缓存机制;第6节 DAG的生成

    4.      RDD的依赖关系 6.1      RDD的依赖 RDD和它依赖的父RDD的关系有两种不同的类型,即窄依赖(narrow dependency)和宽依赖(wide dependency ...

  2. 树莓派4B踩坑指南 - (4)输入法和字体

    输入法和字体 fcitx 安装谷歌输入法和sunpinyin,哪个不用可以装完卸载: sudo apt-get install fcitx fcitx-googlepinyin fcitx-modul ...

  3. 实现JSP部分内容继承

    我们的网站框架搭好以后,只需要主体部分显示不同的数据. 如果每次代码重写都会造成冗余. 今天欣赏别人代码,学到了 maven 核心代码 <dependency> <groupId&g ...

  4. Mysql 锁定 读情况

    在一个事务中,标准的SELECT语句是不会加锁,但是有两种情况例外. SELECT ... LOCK IN SHARE MODE SELECT ... FOR UPDATE SELECT ... LO ...

  5. Scrapy采集某小说网站的全部小说

    链接: https://pan.baidu.com/s/1hrgYDzhgQIDrf4KmZxhW1w 密码: h1m6 源码以及运行图

  6. BUU re xor

    从13行和18行的0x21(c规定十六进制必须用0x**表示)可以知道这个字符串就是33个字符 shift+e来提取出数组中的字符: 设这个数组是global数组 global[] = { 102, ...

  7. 【剑指Offer面试编程题】题目1368:二叉树中和为某一值的路径--九度OJ

    题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 输入: 每个测试案例包括n+1行: 第一行为2 ...

  8. 更改windows系统的快捷键方法

    众所周知,windows平台有很多快捷键使用非常的别扭. 现在提供windows 平台快捷键替换的绝佳软件:autohotkey 下载链接:http://ahkscript.org/ 中文帮助站点:h ...

  9. Prometheus 学习目录

    Prometheus 介绍 Prometheus 安装 https://www.bookstack.cn/read/prometheus-book/quickstart-why-monitor.md ...

  10. java的基本类型和对应的封装类

    封装,是java这门语言的重要核心思想之一,封装也是对面向对象这一思想很好的体现. 在很多情况下,我们需要对数据进行一些转换,如:将一字符串"123"转换成int类型的123,或者 ...