该怎么优化呢?最后一个点怎么也过不了

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

BILL666 @ 2018-05-29 18:20:14

#include<bits/stdc++.h>
#define MAX -2147483648
#define MIN 2147483647
using namespace std;
int n,k;
int a[1000005];
struct MMM{
    int l,r,mi,ma;
}tree[4000005];
map<int,int> ans;
struct sao{
    int m1,m2;
};
inline void read(int &x)
{
    char c;
    int flag=0;
    while(!isdigit(c=getchar()))
        if(c=='-') flag=1;
    x=c-'0';
    while(isdigit(c=getchar()))
        x=x*10+c-'0';
    if(flag) x=-x;
    return;
}
void Build(int root,int L,int R)
{
    tree[root].l=L;
    tree[root].r=R;
    tree[root].ma=MAX;
    tree[root].mi=MIN;
    if(L==R)
    {
        tree[root].mi=a[L];
        tree[root].ma=a[L];
        return;
    }
    int mid=(L+R)>>1;
    Build(root<<1,L,mid);
    Build(root<<1|1,mid+1,R);
    tree[root].mi=min(tree[root<<1].mi,tree[root<<1|1].mi);
    tree[root].ma=max(tree[root<<1].ma,tree[root<<1|1].ma);
    return;
}
sao Query(int root,int L,int R)
{
    if(L<=tree[root].l&&tree[root].r<=R)
        return (sao){tree[root].mi,tree[root].ma};
    int mid=(tree[root].l+tree[root].r)>>1;
    sao res1,res2;
    res1.m1=MIN;
    res1.m2=MAX;
    res2.m1=MIN;
    res2.m2=MAX;
    if(L<=mid) res1=Query(root<<1,L,R);
    if(mid<R) res2=Query(root<<1|1,L,R);
    return (sao){min(res1.m1,res2.m1),max(res1.m2,res2.m2)};
}
int main()
{
    read(n);
    read(k);
    for(int i=1;i<=n;i++)
        read(a[i]);
    if(k==1)
    {
        for(int i=1;i<=n;i++)
            cout<<a[i]<<" ";
        cout<<endl;
        for(int i=1;i<=n;i++)
            cout<<a[i]<<" ";
        return 0;
    }
    Build(1,1,n);
    for(int i=1,j=1;i+k-1<=n;i++)
    {
        sao x=Query(1,i,i+k-1);
        cout<<x.m1<<" ";
        ans[j++]=x.m2;
    }
    cout<<endl;
    for(int i=1;i<=n-k+1;i++)
        cout<<ans[i]<<" ";
    return 0;
}

by BILL666 @ 2018-05-29 18:21:43

我只是想尽办法想用线段树做出来


by BILL666 @ 2018-05-29 18:22:22

求助!!!


by Kalista @ 2018-05-29 18:26:14

事实上,您可以尝试着使用一些玄学优化,比如循环中在int前加上register,并且使用位运算一类,同时放弃cout,换用printf或者直接快输。


by かなで @ 2018-05-29 18:27:16

@BILL666 加快写估计就过了


by panda_2134 @ 2018-05-29 18:50:36

zkw吧,其实正解单调队列。。。


by BILL666 @ 2018-05-29 19:28:40

THANKS


|