并不对劲的st表
对于带修改的区间求和能做到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表的更多相关文章
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- 【BZOJ-2006】超级钢琴 ST表 + 堆 (一类经典问题)
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2473 Solved: 1211[Submit][Statu ...
- 【BZOJ-3956】Count ST表 + 单调栈
3956: Count Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 173 Solved: 99[Submit][Status][Discuss] ...
- 【BZOJ-4569】萌萌哒 ST表 + 并查集
4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 459 Solved: 209[Submit][Status] ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- HDU5726 GCD(二分 + ST表)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- Bzoj 2006: [NOI2010]超级钢琴 堆,ST表
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2222 Solved: 1082[Submit][Statu ...
- ST表poj3264
/* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...
随机推荐
- awk,grep,sed文本格式化处理
一.awk取列 [root@web01 ~]# cat /etc/passwd|awk -F ':' '{print $1"\t\t"$7}' ###-F指定分隔符 root /b ...
- Qt 多语言支持
简介 Qt 多语言支持很强大,很好用. 首先要强调的是程序中需要翻译的字符串最好都用 tr("message") 这种形式,这里的 "message" 就是需要 ...
- js获取指定字符前/后的字符串简单实例
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 60. Spring Boot写后感【从零开始学Spring Boot】
从2016年4月15日到2016年7月20日经历长达3个月的时间,[从零开始学习Spring Boot]系列就要告一段落了.国内的各种资源都比较乱或者是copy 来copy去的,错了也不加以修正下,导 ...
- stl lower_bound()返回值
http://blog.csdn.net/niushuai666/article/details/6734403 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回 ...
- 基于HttpClient4.5.2实现的HttpClient工具类
1.maven依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>co ...
- vim状态栏的扩充
将以下内容添加到~/.vimrc文件中: set statusline= set statusline+=%7*\[%n] " ...
- POJ2367 拓扑排序 裸题 板子题
http://poj.org/problem?id=2367 队列版 #include <stdio.h> #include <math.h> #include <str ...
- POJ 3320_Jessica's Reading Problem
题意: 每页书都对应一个知识点,问最少看连续的多少页,才能把所有知识点都看完? 分析: <挑战程序设计竞赛>介绍的尺取法,反复推进区间的开头和结尾,来求取满足条件的最小区间,先确定好一个满 ...
- HDD
硬盘 SCSI ★ Host adapter ★ SCSI standard ★ Bus socket ★ Signal fashion ★ SCAM ★ Bus main control ★ Dri ...