分块70分,扎心了-_-||

P1440 求m区间内的最小值

肥婆纳妾 @ 2019-08-26 12:15:49

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+6;
int bsz,n,m;
int tag[10000],bl[maxn],v[maxn];
inline int read(){
    int x=0,w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
inline void write(int x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
void build(){
    bsz=sqrt(n);
    memset(tag,127,sizeof(tag));
    for(register int i=1;i<=n;++i)v[i]=read();
    for(register int i=1;i<=n;++i)bl[i]=(i-1)/bsz+1;
    for(register int i=1;i<=n;++i)tag[bl[i]]=min(tag[bl[i]],v[i]);
   // for(register int i=0;i<=10;++i)cout<<tag[i]<<" ";
}
inline int ask (int l,int r ){
    if(l<1)l=1;
    if(r<1)return 0;
    int ans=maxn;
    for(register int i=l;i<=min(bl[l]*bsz,r);++i)
        ans=min(ans,v[i]);
    if(bl[l]!=bl[r])
        for(register int i=(bl[r]-1)*bsz+1;i<=r;++i)
            ans=min(ans,v[i]);
    for(register int i=bl[l]+1;i<=bl[r]-1;++i)
        ans=min(ans,tag[i]);
    return ans;
}
int main(){
    n=read(),m=read();
    build();
    for(register int i=1;i<=n;++i)
        write(ask(i-m,i-1)),putchar('\n');
    return 0;
}

by Hexarhy @ 2019-08-26 12:16:28

为什么要用分块……

QWQ


by EnochWenzhou @ 2019-08-26 12:22:44

dddl,线段树,st表都比分块简单


by Juan_feng @ 2019-08-26 12:36:43

普通分块建议放弃( 分块树 分块st倒是可以跑进最优解第一页


by Space_Gold_Trash @ 2019-10-28 23:36:39

我RMQ内存爆了QwQ


|