BZOJ1636&&1699:[USACO2007JAN]Balanced Lineup
浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1636
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1699
裸的\(RMQ\)
时间复杂度:\(O(nlogn+m)\)
空间复杂度:\(O(nlogn)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5e4+5;
int n,m;
int a[maxn],Log[maxn];
int f[17][maxn],g[17][maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
void make_st() {
Log[0]=-1;
for(int i=1;i<=n;i++)
Log[i]=Log[i>>1]+1;
for(int i=1;i<17;i++)
for(int j=1;j+(1<<i)-1<=n;j++) {
f[i][j]=max(f[i-1][j],f[i-1][j+(1<<(i-1))]);
g[i][j]=min(g[i-1][j],g[i-1][j+(1<<(i-1))]);
}
}
int query(int l,int r) {
int x=Log[r-l+1];
int mx=max(f[x][l],f[x][r-(1<<x)+1]);
int mn=min(g[x][l],g[x][r-(1<<x)+1]);
return mx-mn;
}
int main() {
n=read(),m=read();
for(int i=1;i<=n;i++)
f[0][i]=g[0][i]=a[i]=read();
make_st();
for(int i=1;i<=m;i++) {
int l=read(),r=read();
printf("%d\n",query(l,r));
}
return 0;
}
BZOJ1636&&1699:[USACO2007JAN]Balanced Lineup的更多相关文章
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...
- 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队
[算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树
题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...
随机推荐
- 大华NVR设备接分别入宇视摄像机Onvif和RTSP主子码流的方案说明
需求提要 1.各个内网现场有多种网络摄像机IPC和网络硬盘录像机NVR设备: 2.需要将这些设备统一接入到云端中心平台,进行统一的视频直播和录像回放管理: 3.由于目前IPC设备都属于高清设备,主码流 ...
- visual studio2017 无法添加引用 未能加载包ReferenceManagerPackage not such interface support 解决方法
安装完visual studio 2017 后添加引用总是提示 未能加载包ReferenceManagerPackage, 这个问题困扰了两天,直到在网上看到了下面这一段 I just got thi ...
- 用VirtualBox和vagrant在win7×64上搭建ruby on rails 开发环境
下载准备 1.vagrant 官方 WINDOWS Universal (32 and 64-bit) http://www.vagrantup.com/downloads.html 2.Virtu ...
- web前端开发-Ajax(2)
前面的一篇博文简单的简绍了Ajax基于jQuery的用法,接下来要对Ajax做进一步的介绍,Ajax请求大致可以通过三种方式发送:原生Ajax,jQuery,伪Ajax.1.原生Ajax: 由于Aja ...
- ThinkPHP5.0 用docker部署
Dockerfile 文件如下: FROM hub.c.163.com/shenggen/thinkphp-docker:v0.0.1 ADD . /app RUN ["chmod" ...
- eclipse revert resources 很慢的解决办法
eclipse启动无响应,停留在Loading workbench状态,或老是加载不了revert resources 做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应 ...
- 谷歌浏览器chrome上的firepath ----chropath
图标如下 功能:获取相对xpath,绝对xpath和CSS选择器.在devtools面板中编辑,检查并验证XPath和CSS选择器. 使用方法
- log4j2.xml的例子
项目中用到的一个log4j2.xml的配置文件: <?xml version="1.0" encoding="UTF-8"?> <!--设置l ...
- python 3 并发编程之多进程 multiprocessing模块
一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...
- 本地储存(localStorage)记录
- 本地存储 + localStorage.getItem("search_history") 获取本地存储 + localStorage.setItem("a" ...