ST表
细节
用于查询区间最大最小值,不便于进行修改
模板
P3865 【模板】ST 表
查找区间最大值
#include<bits/stdc++.h>
using namespace std;
const int logn = 21;
const int maxn = 2e6+1;
int f[maxn][logn+1], Logn[maxn+1];
inline int read() {
char c = getchar();
int x = 0, f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x*f;
}
void pre() { // 预处理不同区间长度需要跨越的格数
Logn[1] = 0; // 第一列是 2^0 = 1 本身
Logn[2] = 1; // 第二列是 2^1 = 2 向后跳跃2个位
for(int i = 3; i < maxn; i++) {
Logn[i] = Logn[i/2] + 1; // 倍增跳跃,比自己除2(向下取整)的区间长度多跳1
}
}
int main() {
int n = read(), m = read();
for(int i = 1; i <= n; i++) f[i][0] = read(); // 存一列需要处理的数
pre();
for(int j = 1; j <= logn; j++)
for(int i = 1; i + (1<<j) - 1 <= n; i++) // 区间不能 >n
f[i][j] = max(f[i][j-1], f[i + (1 << (j-1))][j-1]); // 取两个半区间 // (i,i+2^(j-1)-1) (i+2^(j-1),i+2^j-1
for(int i = 1; i <= m; i++) {
int x = read(), y = read();
int s = Logn[y-x+1]; // 对应向下取整的格数,所以不会超出询问范围,但是两个区间可以完全覆盖询问区间
printf("%d\n", max(f[x][s], f[y - (1<<s) + 1][s]));
}
return 0;
}
例题
- #2279. 「SCOI2007」降雨量
一下为错误代码,我认为这个题难在判断而不是ST表本身,所以先留着,等以后熟练了再修正
#include<bits/stdc++.h>
using namespace std;
#define update(a,b) memset(a, b, sizeof(a))
const int maxn = 5e4+10;
const int logn = 21;
typedef long long ll;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x*f;
}
int n, year[maxn], r[maxn], m, st[maxn][logn+1], Logn[maxn];
inline int query(int x, int y) {
if(x > y) return -1e9;
int s = Logn[y-x+1];
return max(st[x][s], st[y-(1<<s)+1][s]);
}
int Judge(int x, int y) {
int fx = lower_bound(year+1, year+n+1,x)-year, fy = lower_bound(year+1,year+n+1,y)-year;
int bx = year[fx], by = year[fy], q;
if(bx) {
if(by) {
q = query(fx+1,fy-1);
if(r[fx] < r[fy]) return 0;
if(q < r[fy]) {
if(fy-fx == y-x) return 2; // 避免年份不存在
return 1; // 不存在则maybe
}
return 0;
}
else {
q = query(fx+1, fy-1);
if(q < r[fx]) return 1; // 如果查询区间左端点r[x]> q,则
return 0;
}
}
else{
if(by){
q = query(fx, fy - 1);
if(q < r[fy]) return 1; // 左端点不大于区间最大值,maybe
return 0;
}
else return 1;
}
}
void pre() {
Logn[0] = -1;
for(int i = 1; i < logn; i++){
Logn[i] = Logn[i/2]+1;
}
}
int main() {
n = read();
pre();
for(int i = 1; i <= n; i++) year[i] = read(), r[i] = read(), st[i][0] = read();
for(int j = 1; j <= logn; 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]);
}
}
m = read();
while(m--) {
int y = read(), x = read();
int flag = Judge(y, x);
if(flag == 0) puts("false");
else if(flag == 1) puts("maybe");
else puts("true");
}
return 0;
}
题意:
求区间max , min的差值
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+1;
const int logn = 21;
int Logn[maxn+1];
int ffmin[maxn][logn], ffmax[maxn][logn];
inline int read() {
char c = getchar();
int x = 0, f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c =getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return f*x;
}
void pre() {
Logn[1] = 0;
Logn[2] = 1;
for(int i = 3; i < maxn; i++) {
Logn[i] = Logn[i/2] + 1;
}
}
int main () {
int n, m;
n = read(), m = read();
for(int i = 1; i <= n; i++) ffmin[i][0] = ffmax[i][0] = read();
pre();
for(int j = 1; j <= logn; j++) {
for(int i = 1; i + (1 << j) -1 <= n; i++) {
ffmax[i][j] = max(ffmax[i][j-1], ffmax[i + (1 << (j-1))][j-1]);
ffmin[i][j] = min(ffmin[i][j-1], ffmin[i + (1 << (j-1))][j-1]);
}
}
for(int i = 1; i <= m; i++) {
int x = read(), y = read();
int s = Logn[y-x+1];
printf("%d\n", max(ffmax[x][s], ffmax[y-(1<<s)+1][s]) - min(ffmin[x][s], ffmin[y-(1<<s)+1][s]));
}
return 0;
}