数据结构-模板-ST表

预处理时间复杂度为O(nlogn),查询时间复杂度O(1)

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN=1e18;
const int MOD=1e6;

struct ST{
    int el[100050][21],n;
    void init(){
        for(int i=1;i<=20;++i){
            for(int j=1;j+(1<<i)-1<=n;++j){
                el[j][i]=max(el[j][i-1],el[j+(1<<(i-1))][i-1]);
            }
        }
    }
    int query(int l,int r){
        int len=log2(r-l+1);
        return max(el[l][len],el[r-(1<<len)+1][len]);
    }
}st;

int main(){
    IOS;
    int m;
    cin>>st.n>>m;
    for(int i=1;i<=st.n;++i){
        cin>>st.el[i][0];
    }
    st.init();
    int r,l;
    while(m--){
        cin>>l>>r;
        cout<<st.query(l,r)<<'\n';
    }
    return 0;
}
posted @ 2021-04-11 15:02  七铭的魔法师  阅读(50)  评论(0编辑  收藏  举报