浅谈\(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的更多相关文章

  1. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  2. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  3. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  4. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  5. POJ 3274:Gold Balanced Lineup 做了两个小时的哈希

    Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13540   Accepted:  ...

  6. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

  7. 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队

    [算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...

  8. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  9. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

随机推荐

  1. 【BZOJ2115】[Wc2011] Xor 高斯消元求线性基+DFS

    [BZOJ2115][Wc2011] Xor Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ...

  2. java操作文件流对象

    所有流对象 InputStream 字节流         FileInputStream 字节流 专门读写非文本文件的         BufferedInputStream 高效流 OutPutS ...

  3. web.xml配置中的log4jRefreshInterval

    采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...

  4. Delphi窗体研究,留个爪,以后回来研究

    Delphi - 窗体创建过程   来自大富翁. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  5. [luogu3601]签到题

    [luogu3601]签到题 luogu 求\[\sum_{i=l}^ri-\phi(i)\] 一个朴素的想法是枚举l~r,根号求\(\phi\),显然这样是\((r-l)\sqrt r\),时间无法 ...

  6. java堆分析神器MAT

    Memory Analyzer(MAT) 基于Eclipse的软件 http://www.eclipse.org/mat/

  7. RxJava2 源代码解析(一)

    转载请标明出处: http://blog.csdn.net/zxt0601/article/details/61614799 本文出自:[张旭童的博客](http://blog.csdn.net/zx ...

  8. 在线工具集合(新增cron quartz表达式在线生成……)

    缘起 平时工作,须要一些工具.经过一些使用,对照,保留一些比較方便好用的在线工具 工具会持续更新中.. . 在线编译&&反编译  http://www.showmycode.com/ ...

  9. LeetCode:删除排序链表中的重复元素【83】

    LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...

  10. HDU - 1033 Edge 【模拟】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1033 题意 给定一个起始点 300 420 走的第一步是 310 420 下面的每一步 都由 输入决定 ...