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 ...
随机推荐
- Scout YYF I (概率+矩阵快速幂)
YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's ba ...
- java List的相关工具类
1. <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</ar ...
- Android系统移植与调试之------->如何修改Android设备的桌面背景图片
1.切换到~/mx0831-0525/device/other/TBDG1073/overlay/frameworks/base/core/res/res目录 2.准备好一张相应尺寸的图片并且命名为d ...
- Cocos2d-x 3.1 环境搭建和创建project
Cocos2d-x 3.x改版了非常多,之前搭过一次环境,可是没截图.这次趁着重装电脑,一边搭建一边截图.此博文仅仅是为了记录而不是为了教学,所以很多其它讲的是搭建过程.本文基本上參考这篇博客:htt ...
- GeekforGeeks Trie - 键树简单介绍 - 构造 插入 和 搜索
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 磁盘检测SMART工具
题记: 做过一些关于硬盘的调研任务,当时搜集很多资料,不过现在没有,从网上找了一篇关于SMART的介绍,感觉基本上都是比较全面了. 首先各大硬盘厂商生产的硬盘基本都是会遵循SMART的技术标准的,当然 ...
- php函数指定默认值的方法
发布:JB02 来源:脚本学堂 [大 中 小] 本文介绍下,在php编程中,指定函数的默认值的方法,分享二个例子,供大家学习参考下.本文转自:http://www.jbxue.com/ar ...
- Effective java -- 7 通用程序设计
第四十五条:将局部变量的作用域最小化 第四十六条:加强版for循环优于传统for循环 第四十七条:了解和使用类库书中提到了一个生成随机数的例子.正好需要. public static void mai ...
- [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 【leetcode刷题笔记】Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...