跳到正文

个人记录

st表の模版

gyf_QAQ约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

蒟蒻写的st表(自认为码风良好

C++
#include <bits/stdc++.h>
using namespace std;
const int max_=2e6+5;
int n,m,lg[max_],st[max_][100];
int f(int x,int y){//f(x,x)=x
	return max(x,y);
}
signed main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>st[i][0];
    }
    lg[1]=0;
    for(int i=2;i<=n;i++){
        lg[i]=lg[i>>1]+1;
    }
    for(int j=1;j<=lg[n];j++){
        for(int i=1;i<=(n-(1<<j)+1);i++){
            st[i][j]=f(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
    }
    while(m--){
        int x,y;
        cin>>x>>y;
        cout<<f(st[x][lg[y-x+1]],st[y-(1<<lg[y-x+1])+1][lg[y-x+1]])<<'\n';
    }
    return 0;
}
原文