数列区间最大值
Sparse Table 区间最大
正文
// 原题:https://oj.yecheng.tv/p/T1541
// 题意:N 个数、M 次询问;每次问区间[X,Y]最大值;输出 M 行各一个数。
// 思路:Sparse Table 预处理区间最大值,O(1) 回答每个询问。
// 复杂度:O(N log N + M) 时间 / O(N log N) 空间
// 易错点:下标 1-based;询问区间 [X,Y] 右端含 Y。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=100005, LOG=17;
int st[MAXN][LOG+1], lg[MAXN];
int main(){
int N,M;
if(!(cin>>N>>M))return 0;
lg[1]=0;
for(int i=2;i<=N;i++)lg[i]=lg[i/2]+1;
for(int i=1;i<=N;i++)cin>>st[i][0];
for(int j=1;j<=LOG;j++)
for(int i=1;i+(1<<j)-1<=N;i++)
st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
while(M--){
int X,Y;cin>>X>>Y;
int k=lg[Y-X+1];
cout<<max(st[X][k],st[Y-(1<<k)+1][k])<<"\n";
}
return 0;
}
一本通·提高篇的其它内容
- 活动安排
- 种树
- 喷水装置
- 加工生产调度
- 智力大冲浪
- 数列极差
- 数列分段
- 线段
- 家庭作业
- 钓鱼
- 糖果传递
- 愤怒的牛
- Best Cow Fences
- 曲线
- 数列分段 II
- 扩散
- 灯泡
- 传送带
- 数的划分
- 生日蛋糕
- 小木棍
- Addition Chains
- 埃及分数
- 平板涂色
- 质数方阵
- 靶形数独
- 电路维修
- 魔板
- Knight Moves
- 棋盘游戏
- Keyboarding
- 移动玩具
- 山峰和山谷
- Oulipo
- 图书管理
- Power Strings
- Seekthe Name, Seek the Fame
- Friends
- A Horrible Poem
- Beads
- Antisymmetry
- 门票
- 收集雪花
- 剪花布条
- Power Strings
- Radio Transmission
- OKR-Periods of Words
- 似乎在梦中见过的样子
- Censoring
- Phone List
- The XOR Largest Pair
- Nikitosh 和异或
- Immediate Decodability
- L 语言
- Secret Message 秘密信息
- 背单词
- The Xor-longest Path
- Keywords Search
- 玄武密码
- Censoring