对于带修改的区间求和能做到O(n log n)预处理,O(log n)查询;而不带修改的可以做到O(n)预处理,O(1)查询。那么不带修改的区间最值能做到O(1)查询吗?

区间最值有这样一个性质:对于一段区间的两个子区间,如果它们覆盖了整个区间(可以有重叠部分),那么这两段区间各自的最大(或最小)值的最大(或最小)值就等于整个区间的最大(或最小)值。

这样的话,可以倍增地求出从每个位置开始的2的x次方的区间最值,预处理每个数的log。查询l-r这一段区间时按如图所示的方式合并就行了。

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxn 100010
using namespace std;
int read()
{
int f=1,x=0;char ch=getchar();
while(isdigit(ch)==0 && ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
int ff=0;char ch[15];
if(x<0)
{
x=-x;
putchar('-');
}
while(x)ch[++ff]=(x%10)+'0',x/=10;
if(ff==0)putchar('0');
while(ff)putchar(ch[ff--]);
putchar('\n');
}
struct ST
{
int st[maxn][20],logx[maxn],two[maxn];
int n,q,l,r;
void ask()
{
l=read(),r=read();
write(max(st[l][logx[r-l+1]],st[r-two[r-l+1]+1][logx[r-l+1]]));
}
void work()
{
n=read(),q=read();
for(int i=1,j=1,k=0;i<=n;i++)
{
st[i][0]=read();
if((j<<1)<=i)j<<=1,k++;
logx[i]=k;
two[i]=j;
}
for(int i=1;(1<<i)<=n;i++)
{
for(int j=1;j+(1<<(i))-1<=n;j++)
{
st[j][i]=max(st[j][i-1],st[j+(1<<(i-1))][i-1]);
// cout<<st[j][i]<<" ";
}//cout<<endl;
}
while(q--)
{
ask();
}
}//1 2 3 4 5
}t;
int main()
{
t.work();
return 0;
}/*
8 8
9 3 1 7 5 6 0 8
1 6
1 5
2 7
2 6
1 8
4 8
3 7
1 8
*/

  

并不对劲的st表的更多相关文章

  1. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  2. 【BZOJ-2006】超级钢琴 ST表 + 堆 (一类经典问题)

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2473  Solved: 1211[Submit][Statu ...

  3. 【BZOJ-3956】Count ST表 + 单调栈

    3956: Count Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 99[Submit][Status][Discuss] ...

  4. 【BZOJ-4569】萌萌哒 ST表 + 并查集

    4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 459  Solved: 209[Submit][Status] ...

  5. 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分

    4310: 跳蚤 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 180  Solved: 83[Submit][Status][Discuss] De ...

  6. HDU5726 GCD(二分 + ST表)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...

  7. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  8. Bzoj 2006: [NOI2010]超级钢琴 堆,ST表

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2222  Solved: 1082[Submit][Statu ...

  9. ST表poj3264

      /* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...

随机推荐

  1. c++基础_矩阵乘法

    #include <iostream> using namespace std; int main(){ int a,b; cin>>a>>b; long c[a] ...

  2. DFS求连通块(漫水填充法)

    G - DFS(floodfill),推荐 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...

  3. css伪类实现文字两侧划线效果

    css伪类实现文字两侧划线效果,效果如下: 代码如下: <!DOCTYPE HTML> <html> <head> <title> css伪类的学习 & ...

  4. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...

  5. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

  6. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  7. Flask组件:flask-sqlalchemy & flask-script & flask-migrate

    flask-sqlalchemy组件 项目目录结构: flask目录 # 项目名 |--- flaskdir |--- static # 静态文件 |--- templates # 模板 |--- m ...

  8. [K/3Cloud] 创建一个业务单据表单插件

    概念 创建一个业务单据插件,处理单据的相关控制逻辑. 示例 新建一个类,继承自单据插件基类Kingdee.BOS.Core.Bill.PlugIn.AbstractBillPlugIn. using ...

  9. Linux下汇编语言学习笔记26 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  10. 【编程大系】Java资源汇总

    1.学习资料: 1)Spring Boot 那些事:https://www.w3cschool.cn/springboot/ 对应的 gitHub代码: https://github.com/Jeff ...