st表模板

求区间最大值

#include<bits/stdc++.h>
#define re return
#define ll long long 
#define inc(i,l,r) for(int i=l;i<=r;++i)

using namespace std;
template<typename T>inline void rd(T&x)
{
    char c;bool f=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')f=1;
    x=c^48;
    while((c=getchar())>='0'&&c<='9')x=x*10+(c^48);
    if(f)x=-x;
}
const int maxn=1e6+5;
int Log[maxn],f[maxn][25],a[maxn];
int n,m;
int main()
{
    
    freopen("in.txt","r",stdin);
    int x,y;
    rd(n),rd(m);
    inc(i,1,n)
        rd(f[i][0]);//初始化 
    
    //得到log 
    Log[0]=-1;//使log[1]=0;
    inc(i,1,n)
    Log[i]=Log[i>>1]+1;
     
     
    inc(j,1,Log[n])
    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]);
    

    inc(i,1,m)
    {
        rd(x),rd(y);
        int k=Log[y-x+1];
        printf("%d\n",max(f[x][k],f[y-(1<<k)+1][k]));
    }
    
    re 0;
}

 

posted @ 2019-08-21 10:10  凉如水  阅读(242)  评论(0编辑  收藏  举报