80求调

P1440 求m区间内的最小值

xzq4121 @ 2024-06-28 20:42:36

#include <bits/stdc++.h>
using namespace std;
long long n,m,p[4000100],shu[4000100];
void lan(long long h,long long mid,long long s,long long t){
    shu[h*2]+=p[h];
    shu[h*2+1]+=p[h];
    p[h*2]+=p[h];
    p[h*2+1]+=p[h];
    p[h]=0;
}
void jianshu(long long s,long long t,long long b){
    if(s==t){
        shu[b]=p[s];
        return;
    }
    long long mid=(s+t)/2;
    jianshu(s,mid,b*2);
    jianshu(mid+1,t,b*2+1);
    shu[b]=min(shu[b*2],shu[b*2+1]);
}
long long cha(long long l,long long r,long long s,long long t,long long h){
    if(l<=s&&t<=r) return shu[h];
    long long mid=(s+t)/2,ans=0,a=0;
    if(p[h]) lan(h,mid,s,t);
    if(l<=mid) ans=cha(l,r,s,mid,h*2),a=1;
    if(r>mid){
        if(a==0) ans=cha(l,r,mid+1,t,h*2+1);
        else ans=min(cha(l,r,mid+1,t,h*2+1),ans);
    } 
    return ans;
}
signed main(){
    scanf("%lld %lld",&n,&m);
    for(long long i=1;i<=n;i++) scanf("%d",&p[i]);
    jianshu(1,n,1);
    memset(p,0,sizeof(p));
    printf("0\n");
    for(long long i=2;i<=n;i++){
        printf("%lld\n",cha(max(1,int(i-m)),i-1,1,n,1));
    }
    return 0;
} 

by xzq4121 @ 2024-06-28 20:43:10

用的线段树


by Misophiliac @ 2024-06-28 21:00:32

@xzq4121 数组开到 2^{22}


by xzq4121 @ 2024-06-28 21:12:53

@Misophiliac 感谢,已过,已关注


|