#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int f[100005][22];
int main(){
int n,m; scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&f[i][0]);
for(int j=1;j<=20;j++) //枚举区间长度
for(int i=1;i+(1<<j)-1<=n;i++) //枚举起点
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
for(int i=1,l,r;i<=m;i++){
scanf("%d%d",&l,&r);
int k=log2(r-l+1); //区间长度的指数
printf("%d\n",max(f[l][k],f[r-(1<<k)+1][k]));
}
}
建议配合快读
int read() { //fast read
int x=0,f=1;
char c=getchar();
while(c<'0' || c>'9') { //!isdigit(c)
if(c=='-') f=-1;
c=getchar();
}
while(c>='0' && c<='9') { //isdigit(c)
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
ST表+快读
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int read() { //fast read
int x=0,f=1;
char c=getchar();
while(c<'0' || c>'9') { //!isdigit(c)
if(c=='-') f=-1;
c=getchar();
}
while(c>='0' && c<='9') { //isdigit(c)
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int f[100005][22];
int main(){
int n,m;
n=read();
for(int i=1;i<=n;i++) f[i][0]=read();
for(int j=1;j<=20;j++) //枚举区间长度
for(int i=1;i+(1<<j)-1<=n;i++) //枚举起点
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
m=read();
for(int i=1,l,r;i<=m;i++){
l=read();
r=read();
int k=log2(r-l+1); //区间长度的指数
printf("%d\n",max(f[l][k],f[r-(1<<k)+1][k]));
}
}
标签:RMQ,int,while,ST,read,最小值,include,getchar
From: https://www.cnblogs.com/swjswjswj/p/18359664