数据结构:ST表模板(可维护区间RMQ)

点击查看折叠代码块
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;
const int maxn=1e5+10;
int a[maxn][30],logg[maxn],bin[maxn];
int i,j,k,m,n,o,p,js,jl,x,y,t,lg;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int my_max(int x,int y){
    if(x>y)return(x);
    else return(y);
}
int main(){
    // freopen("1.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) a[i][0]=read();

    bin[0]=1;
    for(int i=1;i<=30;i++)
        bin[i]=bin[i-1]*2;
    
    logg[0]=-1;
    for(int i=1;i<=n;i++)
        logg[i]=logg[i/2]+1;

    for(int i=1;i<=logg[n];i++){
        for(int j=1;j<=n;j++){
            if(j+bin[i]-1<=n){
                a[j][i]=my_max(a[j][i-1],a[j+bin[i-1]][i-1]);
            }
        } 
    }

    for(int i=1;i<=m;i++){
        x=read();y=read();
        t=y-x+1;
        lg=logg[t];
        printf("%d\n",my_max(a[x][lg],a[y-bin[lg]+1][lg]));
    }
    return 0;
}
posted @ 2020-07-28 20:37  wsl_lld  阅读(76)  评论(0编辑  收藏  举报