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

题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问

思路:ST表模板,预处理区间最值,O(1)复杂度询问

AC代码:

 #include<iostream>
#include<vector>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = ;
const int logn = ;
int Log[maxn];
int MIN[maxn][logn];
int MAX[maxn][logn];
int cow[maxn];
int n,q;
void pre(){
Log[] = ;
Log[] = ;
for(int i = ;i<=n;i++){
Log[i] = Log[i/] + ;
}
}
void st(){
for(int i = ;i<=n;i++){
MIN[i][] = MAX[i][] = cow[i];
}
for(int j = ;(<<j)<=n;j++){
for(int i = ;i+(<<j)-<=n;i++){
MAX[i][j] = max(MAX[i][j-],MAX[i+(<<(j-))][j-]);
MIN[i][j] = min(MIN[i][j-],MIN[i+(<<(j-))][j-]);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i = ;i<=n;i++){
scanf("%d",&cow[i]);
}
pre();
st();
while(q--){
int A,B;
scanf("%d%d",&A,&B);
int s = Log[B-A+];
int range_max = max(MAX[A][s],MAX[B-(<<s)+][s]);
int range_min = min(MIN[A][s],MIN[B-(<<s)+][s]);
cout<<range_max-range_min<<endl;
}
return ;
}

POJ 3264 Balanced Lineup(ST模板)的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

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

  2. [POJ] 3264 Balanced Lineup [ST算法]

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

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

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

  4. POJ 3264 Balanced Lineup | st表

    题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...

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

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

  6. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  7. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  8. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  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. CF594D REQ [离线+树状数组,欧拉函数]

    设 \[x = \prod_{i=1}^{cnt} p_i^{k_i} [p_i\in prime]\] 那么显然 \[\varphi(x) = x*\frac{1} {\prod_{i=1}^{cn ...

  2. VSCode(主进程)

    Overview(总览) SETUP(设定) Overview(总览) Linux(略) macOS(略) Windows 使用WSL,您可以在Windows上安装和运行Linux发行版.这使您能够在 ...

  3. VISIO 的一些技巧

    1.复制绘图 如果格式改变,在“设计”选项卡里将“将主题运用于新建的形状”前面的√去掉

  4. webpack4从安装到使用入过的那些坑

    1.全局安装 安装 webpack 前要确保已经下载 node.js 最新版本(node.js官网地址) 目前使用命令行安装的 webpack 默认为最新版本(以下我使用了全局安装.官网建议本地安装, ...

  5. CF895C Square Subsets [线性基]

    线性基的题- 考虑平方数只和拆解质因子的个数的奇偶性有关系 比如说你 \(4\) 和 \(16\) 的贡献都是一样的.因为 \(4 = 2^2 , 16 = 2^4\) \(2\) 和 \(4\) 奇 ...

  6. windows密码抓取工具-mimikatz

    前言 介绍一下windows的密码hash值的组成: Windows系统下的hash密码格式为:用户名称:RID:LM-HASH值:NT-HASH值,例如: Administrator::C8825D ...

  7. H5-IOS能否自动弹出软键盘

    在ios移动端, 弹出软键盘只能 行为事件才能触发 弹出软键盘, 脚本事件是不能触发 弹出软键盘 (行为事件:如点击,触屏等) (脚本事件:如 定时器定时触发)

  8. 用 ArcMap 发布 ArcGIS Server Feature Server Feature Access 服务

    1. 安装Desktop, 2. 安装ArcGIS Server 3. 安装PostgreSQL 9.5 从 C:\Program Files (x86)\ArcGIS\Desktop10.5\Dat ...

  9. ffmpeg rtp rtmp udp 推流命令

    推组播 组播地址指的范围是224.0.0.0—239.255.255.255 ffmpeg -re -i chunwan.h264 -vcodec mpeg2video -f mpeg2video u ...

  10. MyBatis 中 Mapper 接口的使用原理

    MyBatis 中 Mapper 接口的使用原理 MyBatis 3 推荐使用 Mapper 接口的方式来执行 xml 配置中的 SQL,用起来很方便,也很灵活.在方便之余,想了解一下这是如何实现的, ...