收藏一个ST表模板

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

int M,N,x,y;
int Log2[100010],money[100010],st[100010][30];

void Done()
{
    Log2[1]=0,st[1][0]=money[1];
    for(int i=2;i<=M;++i)
    {
        Log2[i]=Log2[i-1];
         if(i==1<<Log2[i-1]+1) ++Log2[i];
        st[i][0]=money[i];
    }
    for(int i=M;i>=1;--i)
       for(int j=1;i+(1<<j)-1<=M;++j)
          st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
int Ask(int l,int r)
{
    int x=Log2[r-l+1];
    return min(st[l][x],st[r-(1<<x)+1][x]);
}

int main()
{
    scanf("%d%d",&M,&N);
    memset(st,63,sizeof(st));
    for(int i=1;i<=M;++i)
       scanf("%d",&money[i]);
    Done();
    while(N--)
    {
        scanf("%d%d",&x,&y);
        printf("%d ",Ask(x,y));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linruier/p/10035292.html