题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min),

用RMQ模板,同时构造求极大值和极小值的两个数组。

 //poj3264
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdio>
#include<cmath>
using namespace std;
const int maxn=;
int s[maxn];
int dpmin[maxn][];//存小值
int dpmax[maxn][];//存大值
int rmq_min(int l,int r)//查询最小值
{
int k=log2(r-l+);
return min(dpmin[l][k],dpmin[r-(<<k)+][k]);
}
int rmq_max(int l,int r)//查询最大值
{
int k=log2(r-l+);
return max(dpmax[l][k],dpmax[r-(<<k)+][k]);
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&s[i]);
for(int i=;i<=n;i++)
{
dpmin[i][]=s[i];//赋初值
dpmax[i][]=s[i];
}
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<=n;i++)
{
dpmin[i][j]=min(dpmin[i][j-],dpmin[i+(<<j-)][j-]);//构建
dpmax[i][j]=max(dpmax[i][j-],dpmax[i+(<<j-)][j-]);
}
}
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",rmq_max(x,y)-rmq_min(x,y));//大减小就是结果
}
return ;
}

POJ3264 Balanced Lineup [RMQ模板]的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

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

  2. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

  3. [USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题

    Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 50000 + ...

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

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

  5. poj 3264 Balanced Lineup (RMQ)

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

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

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

  7. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  8. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  9. POJ3264 Balanced Lineup 【线段树】+【单点更新】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 ...

随机推荐

  1. PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  2. E - Tokitsukaze and Duel CodeForces - 1190C (博弈 + 窗体移动)

    "Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty ha ...

  3. IDEA忽略文件,防止git提交不想提交的文件

    IDEA忽略文件,防止git提交不想提交的文件 方法一(只对没有add到仓库的文件有效): 方法二(只对没有add到仓库的文件有效): 在IDEA中安装.ignore插件.创建好了之后: 安装.git ...

  4. SpringMVC在使用JSON时报错信息为:Content type 'application/json;charset=UTF-8' not supported

    直接原因是:我的(maven)项目parent父工程pom.xml缺少必要的三个jar包依赖坐标. 解决方法是:在web子模块的pom.xml里面添加springMVC使用JSON实现AJAX请求. ...

  5. Anaconda Installation on Mac: conda command not found 环境变量配置

    Mac系统安装完Anaconda 3.7后在terminal输入conda --version,返回command not found 原因可能是没有配置环境变量 在terminal输入vi ~/.b ...

  6. Go-开发环境搭建

    02-开发环境搭建   目录 一 下载地址 二 安装 Linux安装 Windows安装 Mac安装 三 测试安装 四 命令介绍 基本介绍 build 和 run 命令 get 命令 一 下载地址 安 ...

  7. tensorflow C++接口调用图像分类pb模型代码

    #include <fstream> #include <utility> #include <Eigen/Core> #include <Eigen/Den ...

  8. upstream实现内网网站在公网访问

    背景描述:公司内网有个网站aa.com,B部门需要访问这个aa.com,但是网站部署在内网服务器(服务器是192.168.1网段),B部门网段是192.168.100 需求描述:B部门需要访问aa.c ...

  9. 人工智能必备之Python3.8.1-安装

    1_下载Python 2_下载Python 3_下载Python 4_下载Python-选这里下载:Windows x86-64 executable installer 5_安装Python 6.自 ...

  10. PHP 限制访问ip白名单

    一  上代码 config.php //ip白名单配置 'ipWlist'=>[ 'ifFilter'=>true, //是否开启白名单功能 'wlist'=>[ '10.0.0.1 ...