【倍增】【st表模板】洛谷P3865

提起动态区间问题,我们首先想到的肯定是线段树
但是对于静态的区间问题来说,可能有些算法是更加高hao效xie的~
今天来介绍一种叫st表的数据结构,了解一下它的倍增思想

-------------挖个坑,以后补哈~-------------

#include
using namespace std;
typedef long long ll;

const int NN=1000100;
ll n,m,k,x,y,z,q,W,T,N,cnt,tmp,siz,dst,cas;
int Log[NN],w[NN][21];

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //freopen("testdata.txt","r",stdin);
    cin>>n>>m;
    for(int i=2;i<=n;i++) Log[i]=Log[i/2]+1;
    for(int i=1;i<=n;i++) cin>>w[i][0];
    for(int j=1;j<=20;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            w[i][j]=max(w[i][j-1],w[i+(1<<(j-1))][j-1]);
        }
    }
    for(int i=1;i<=m;i++){
        cin>>x>>y;
        int s=Log[y-x+1];
        cout<<max(w[x][s],w[y-(1<<s)+1][s])<<endl;
    }
}

要在洛谷通过还需要加上读入优化
快速读入外挂:

inline int read() {
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isdigit(ch);x=(x<<3)+(x<<1)+(ch^48),ch=getchar());
    return x*f;
}

你可能感兴趣的:(数据结构)