不会吧不会吧

P1440 求m区间内的最小值

云浅知处 @ 2020-07-06 11:38:09

线段树面对 2\times10^6 的空间 居然 MLE 啦?

提交记录

这里是代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

#include<cstdio>
#include<cctype>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>

#define MAXN 2000005
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)

using namespace std;

int a[MAXN],n,m;

inline int read(){
    int w=0;
    char ch=getchar();
    while(!(ch>='0'&&ch<='9'))ch=getchar();
    while(ch>='0'&&ch<='9')w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
    return w;
}

struct SMT{

    int minv[MAXN<<2];

    inline void pushup(int o){
        minv[o]=min(minv[lson(o)],minv[rson(o)]);
    }

    inline void build(int ql,int qr,int o){
        if(ql==qr){
            minv[o]=a[ql];
            return;
        }
        int mid=(ql+qr)>>1;
        build(ql,mid,lson(o));
        build(mid+1,qr,rson(o));
        pushup(o);
    }

    inline int Qrymin(int l,int r,int ql,int qr,int o){
        if(l<=ql&&qr<=r){
            return minv[o];
        }
        int mid=(ql+qr)>>1;
        int ans=233333333;
        if(l<=mid)ans=min(ans,Qrymin(l,r,ql,mid,lson(o)));
        if(r>mid)ans=min(ans,Qrymin(l,r,mid+1,qr,rson(o)));
        return ans;
    }
};

SMT tree;

int main(void){

    n=read(),m=read();

    for(int i=1;i<=n;i++){
        a[i]=read();
    }

    tree.build(1,n,1);

    for(int i=1;i<=n;i++){
        if(i==1&&m!=1){
            printf("0\n");
            continue;
        }
        int j=i-m;
        printf("%d\n",tree.Qrymin(j,i-1,1,n,1));
    }

    return 0;
}

by Chen_cntj @ 2020-07-06 14:55:24

谔谔,单调队列的板子题为什么还要用线段树啊


上一页 |