三个TLE求调

P1440 求m区间内的最小值

Leon4 @ 2024-06-28 14:10:21

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5,LogN=25;
int l[N],f[N][LogN+5],a[N];
int n,m,x,y;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i){
        scanf("%d",&a[i]);
    }
    l[0]=-1;
    for(int i=1;i<=n;++i){
        f[i][0]=a[i],l[i]=l[i>>1]+1;
    }
    for(int j=1;j<=LogN;++j){
        for(int i=1;i+(1<<j)-1<=n;++i){
            f[i][j]=min(f[i][j-1],f[i+(1<<j-1)][j-1]);
        }
    }
    cout<<"0\n";
    for(int i=1;i<n;++i){
        if(i<m)x=1,y=i;
        else x=i-m+1,y=i;
        int s=l[y-x+1];
        printf("%d\n",min(f[x][s],f[y-(1<<s)+1][s]));
    }
    return 0;
}

|