60分 RE 4个

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

MY_KINGDOM @ 2021-02-02 15:51:31

#include<cstdio> 
#define re register
#define ll long long
#define li(i,j,k) for(re int i=j ; i<=k ; i++)
#define si(i,j,k) for(re int i=j ; i>=k ; i--)
const int MAXN=100001;
const int INF=2147483646;
ll n,k;
ll a[MAXN];
struct yzy{
    ll l,r;
    ll minn,maxn;
};
yzy t[MAXN*4];
ll max(ll a,ll b){
    return a>b?a:b;
}
ll min(ll a,ll b){
    return a<b?a:b;
}
void build(ll x,ll l,ll r){
    t[x].l=l,t[x].r=r;
    if(l==r){
        t[x].minn=a[l];
        t[x].maxn=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(x*2,l,mid);
    build(x*2+1,mid+1,r);
    t[x].maxn=max(t[x*2].maxn,t[x*2+1].maxn);
    t[x].minn=min(t[x*2].minn,t[x*2+1].minn);
    return ;
}
ll getmin(ll x,ll l,ll r){
    if(t[x].l>=l && t[x].r<=r){
        return t[x].minn;
    }
    int mid=(t[x].l+t[x].r)>>1;
    int ans=0x3f3f3f3f;
    if(l<=mid)ans=min(ans,getmin(x*2,l,r));
    if(r>=mid+1)ans=min(ans,getmin(x*2+1,l,r));
    return ans;
}
ll getmax(ll x,ll l,ll r){
    if(t[x].l>=l && t[x].r<=r){
        return t[x].maxn;
    }
    int mid=(t[x].l+t[x].r)>>1;
    int ans=-0x3f3f3f3f;
    if(l<=mid)ans=max(ans,getmax(x*2,l,r));
    if(r>=mid+1)ans=max(ans,getmax(x*2+1,l,r));
    return ans;
}
int main(){
    scanf("%lld%lld",&n,&k);
    li(i,1,n)scanf("%lld",&a[i]);
    build(1,1,n);
    li(i,1,n-k+1){
        printf("%lld ",getmin(1,i,i+k-1));
    }
    printf("\n");
    li(i,1,n-k+1){
        printf("%lld ",getmax(1,i,i+k-1));
    }
    return 0;
}

by 霜羽 @ 2021-02-02 15:59:16

n≤10^6

by MY_KINGDOM @ 2021-02-02 16:16:25

谢谢(我是傻逼


|