救救孩子,st表MLE掉了……

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

今天也要开心a @ 2019-11-15 09:11:57

RT

#include<bits/stdc++.h>
using namespace std;
long long r,f,i,j;
char ch;
inline long long read() {
    r=0,f=1;
    ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') r=(r<<1)+(r<<3)+(ch^48),ch=getchar();
    return r*f;
}
long long a[1000005],maxx[1000005][20],minn[1000005][20],lg[1000005],n,m;
int main() {
    lg[0]=-1;
    n=read(),m=read();
    for(i=1;i<=n;++i) a[i]=read(),lg[i]=lg[i/2]+1;
    for(i=1;i<=n;++i) maxx[i][0]=a[i],minn[i][0]=a[i];
    for(i=1;i<=lg[n];++i) 
        for(j=1;j+(1<<i)-1<=n;++j) maxx[j][i]=max(maxx[j][i-1],maxx[j+(1<<(i-1))][i-1]),minn[j][i]=min(minn[j][i-1],minn[j+(1<<(i-1))][i-1]);
    for(i=1;i<=n-m+1;++i) printf("%d ",min(minn[i][lg[m]],minn[m+i-(1<<lg[m])][lg[m]]));
    puts("");
    for(i=1;i<=n-m+1;++i) printf("%d ",max(maxx[i][lg[m]],maxx[m+i-(1<<lg[m])][lg[m]]));
}

by Smile_Cindy @ 2019-11-15 09:13:01

您好,请不要尝试使用ST表通过本题


by 今天也要开心a @ 2019-11-15 09:13:57

@Alpha 这……


by shentao1 @ 2019-11-15 09:15:19

你可以尝试将maxx和minn数组降低一维,毕竟区间长度固定,参见这篇题解


by 今天也要开心a @ 2020-07-31 09:58:52

@shentao1 考自己的古QAQ,顺便感谢(今天才想起来这道题


|