st表总结
基本概念:
st表一般解决的问题是求区间最 大/小 值。为什么不能用单调队列求呢?首先我们要知道,单调队列只是扫一遍,时间复杂度是
st表是一个二维数组,它的原理其实是倍增。
实现流程:
像倍增一样,预处理出来。一开始
接着,预处理像倍增一样,但是有一点不同:
因为
代码(P3865 【模板】ST 表 & RMQ 问题的代码):
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=3e5+5;
int n, m;
int a[N];
int st[N][22];
int lg[N];
signed main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++)
{
cin >> a[i];
st[i][0]=a[i];
}
for(int j=1; j<=18; j++)
{
for(int i=1; i+(1<<(j-1))-1<=n; i++)
{
st[i][j]=max(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
lg[0]=-1;
for(int i=1; i<=n; i++)
{
lg[i]=lg[i/2]+1;
}
while(m--)
{
int l, r;
cin >> l >> r;
int k=lg[r-l+1];
cout << max(st[l][k], st[r-(1<<k)+1][k]) << endl;
}
return 0;
}