企图用ST表水过去

P1886 滑动窗口 /【模板】单调队列

Cobalt @ 2018-08-12 15:15:40

然而MLE了 (这怕是修不好了)

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a[2000001],f[21][2000001],f2[21][2000001],lg[2000001],n,m;
int main(){
    int k;
    scanf("%d%d",&n,&k);
    for(register int i=1;i<=n;i++)
        scanf("%d",a+i);
    lg[0]=-1;
    for(register int i=1;i<=n;i++)
        lg[i]=lg[i/2]+1;
    int t=lg[k],m=(1<<t);
    for(register int i=1;i<=n;i++)
        f[0][i]=f2[0][i]=a[i];
    for(register int i=1;i<=lg[n];i++){
        for(register int j=1;j<=n;j++){
            if(j+(1<<i)-1<=n)
                f[i][j]=max(f[i-1][j],f[i-1][j+(1<<(i-1))]);
        }
    }
    for(register int i=1;i<=lg[n];i++){
        for(register int j=1;j<=n;j++){
            if(j+(1<<i)-1<=n)
                f2[i][j]=min(f2[i-1][j],f2[i-1][j+(1<<(i-1))]);
        }
    }
    for(register int i=1;i<=n-k+1;i++){
        int ans=min(f2[t][i],f2[t][i+k-m]);
        printf("%d ",ans);
    }
    printf("\n");
    for(register int i=1;i<=n-k+1;i++){
        int ans=max(f[t][i],f[t][i+k-m]);
        printf("%d ",ans);
    }
    return 0;
}

by colazcy @ 2018-08-12 15:19:50

砂糖表玄学优化可以水过去(蒟蒻并不会)


by 2016gdgzoi471 @ 2018-08-12 15:36:16

跑两次,废物利用?


by jeffqi @ 2018-08-22 00:24:28

ST表好像要加滚动数组优化才能过


|